如何将html字符串写入html文件并显示 [英] How to write html string into a html file and display it
本文介绍了如何将html字符串写入html文件并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static string ConvertDataTableToHtml(DataTable targetTable)
{
string htmlString = "";
if (targetTable == null)
{
throw new System.ArgumentNullException("targetTable");
}
StringBuilder htmlBuilder = new StringBuilder();
//Create Top Portion of HTML Document
htmlBuilder.Append("<html>");
htmlBuilder.Append("<head>");
htmlBuilder.Append("<title>");
htmlBuilder.Append("Page-");
htmlBuilder.Append(Guid.NewGuid().ToString());
htmlBuilder.Append("</title>");
htmlBuilder.Append("</head>");
htmlBuilder.Append("<body>");
htmlBuilder.Append("<table border="1px" cellpadding="5" cellspacing="0" hold=" /> htmlBuilder.Append(" style="border: solid 1px Black; font-size: small;">");
//Create Header Row
htmlBuilder.Append("<tr align="left" valign="top">");
foreach (DataColumn targetColumn in targetTable.Columns)
{
htmlBuilder.Append("<td align="left" valign="top">");
htmlBuilder.Append(targetColumn.ColumnName);
htmlBuilder.Append("</td>");
}
htmlBuilder.Append("</tr>");
//Create Data Rows
foreach (DataRow myRow in targetTable.Rows)
{
htmlBuilder.Append("<tr align="left" valign="top">");
foreach (DataColumn targetColumn in targetTable.Columns)
{
htmlBuilder.Append("<td align="left" valign="top">");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>");
}
htmlBuilder.Append("</tr>");
}
//Create Bottom Portion of HTML Document
htmlBuilder.Append("</table>");
htmlBuilder.Append("</body>");
htmlBuilder.Append("</html>");
//Create String to be Returned
htmlString = htmlBuilder.ToString();
return htmlString;
}
使用上面的代码,我已经将数据表转换为html格式;现在,我想将这些html字符串写入html文件并显示它.
谁能建议我该怎么做?
在此先感谢您.
Using these above code I have already converted a datatable into html format; Now i want to write these html string into a html file and want to display it.
Can anyone suggest me how to do this?
Thanks in advance.
推荐答案
一个html文件只是一个扩展名为* .html的简单文本文件.因此,只需将字符串写入文本文件-简单.
要将html字符串写入文件,请参见:
http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx#Y0 [^ ]
要使用您喜欢的Web浏览器打开文件,请执行以下操作:
A html file is just a simply text file with the file extension *.html. So, just write the string to a textfile - simple.
To write the html string to a file see:
http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx#Y0[^]
To open the file with your prefered web browser:
System.Diagnostics.Process.Start(fileName);
使用StreamWriter类,并将您的字符串写入扩展名为.htm的文本文件中.
有关使用StreamWriter类的信息,请访问此处 [
Use StreamWriter class and write your string to a text file having .htm extension.
For information about using StreamWriter class, visit here[^]
string filename = "temp.html";
StreamWriter swXLS = new StreamWriter((MapPath("Logs\\")) + filename);
swXLS.Write(html.ToString());
swXLS.Close();
string attachment = "attachment; filename=" + filename;
Response.AddHeader("content-disposition", attachment);
Response.Redirect("~/Officer/Logs/temp.html");
Response.Write(html.ToString());
Response.End();
这篇关于如何将html字符串写入html文件并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文