[C#] 將DataTable轉換成Html格式表格
將DataTable轉換成Html格式表格
using System.Data;
using System.Text;
protected string ExportDatatableToHtml(DataTable dt)
{
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<table border='2px' cellpadding='5'>");
strHTMLBuilder.Append("<tr>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td>");
strHTMLBuilder.Append(myColumn.ColumnName);
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
foreach (DataRow myRow in dt.Rows)
{
strHTMLBuilder.Append("<tr>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td>");
strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
strHTMLBuilder.Append("</table>");
string Htmltext = strHTMLBuilder.ToString();
return Htmltext;
}
留言
張貼留言
歡迎留言