在C#中DataTable导出数据的时刻,我们须要HTML花样的输出数据, 这时刻就须要运用将DataTable导出为到HTML花样的要领了,以下代码就能够协助我们到达目标。
起首,我们要绑定DataTable和 DataGridView。
一、经由过程DataTable绑定DataGridView
1. 建立DataTable,增加列
DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("NAME", typeof(string)); table.Columns.Add("CITY", typeof(string));
2. 再增加行
table.Rows.Add(111, "Devesh", "Ghaziabad"); table.Rows.Add(222, "ROLI", "KANPUR"); table.Rows.Add(102, "ROLI", "MAINPURI"); table.Rows.Add(212, "DEVESH", "KANPUR");
3. 绑定DataGridView
dataGridView1.DataSource=table;
4. 运转效果
二、将DataTable 导出为 HTML
我写了一组代码来为每一个DataTable建立HTML文本。你能够在你的项目中直接援用。
代码以下:
protected string ExportDatatableToHtml(DataTable dt) { StringBuilder strHTMLBuilder = new StringBuilder(); strHTMLBuilder.Append("<html >"); strHTMLBuilder.Append("<head>"); strHTMLBuilder.Append("</head>"); strHTMLBuilder.Append("<body>"); strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>"); 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>"); } //Close tags. strHTMLBuilder.Append("</table>"); strHTMLBuilder.Append("</body>"); strHTMLBuilder.Append("</html>"); string Htmltext = strHTMLBuilder.ToString(); return Htmltext; }
三、代码明白
我们建立了一个函数,运用DataTable作为参数。
然后用stringbuilder类建立动态的HTML文本。
输出效果与DataGridView中的行和列数目雷同。
在HTML中建立列。
foreach (DataColumn myColumn in dt.Columns) { strHTMLBuilder.Append("<td >"); strHTMLBuilder.Append(myColumn.ColumnName); strHTMLBuilder.Append("</td>"); }
复制数据,以下代码就会在DataTable中建立雷同数目的行,并将数据复制到HTML行中。
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>"); }
四、实行上面的代码后获得以下HTML文本
<html > <head> </head> <body> <table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'> <tr > <td >ID</td> <td >NAME</td> <td >CITY</td> </tr><tr > <td >111</td><td >Devesh</td> <td >Ghaziabad</td></tr> <tr ><td >222</td><td >ROLI</td> <td >KANPUR</td></tr><tr > <td >102</td><td >ROLI</td> <td >MAINPURI</td></tr><tr > <td >212</td><td >DEVESH</td> <td >KANPUR</td></tr></table> </body> </html>
五、建立HTML文件
string HtmlBody = ExportDatatableToHtml(table) System.IO.File.WriteAllText(@"c:\abc.HTML", HtmlBody);
六、运转效果
总结
以上就是关于C#中将DataTable导出为HTML花样的全部内容了,愿望本文的内容对人人的进修或许事情能带来肯定的协助,假如有疑问人人能够留言交换。
更多C#中DataTable导出为HTML花样的要领相干文章请关注ki4网!