C中数据导出到Excel表格(12)
发布时间:2021-06-07
发布时间:2021-06-07
DataGridView 数据导出到Excel
六、DataTable导出到excel(2) 1. StringWriter stringWriter = new StringWriter();
2. HtmlTextWriter htmlWriter = new HtmlTextWriter( stringWriter );
3. DataGrid excel = new DataGrid();
4. System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new
TableItemStyle();
5. System.Web.UI.WebControls.TableItemStyle headerStyle = new
TableItemStyle();
6. System.Web.UI.WebControls.TableItemStyle itemStyle = new
TableItemStyle();
7. AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
8. headerStyle.BackColor =System.Drawing.Color.LightGray;
9. headerStyle.Font.Bold = true;
10. headerStyle.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Center;
11. itemStyle.HorizontalAlign =
System.Web.UI.WebControls.HorizontalAlign.Center;;
12.
13. excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
14. excel.HeaderStyle.MergeWith(headerStyle);
15. excel.ItemStyle.MergeWith(itemStyle);
16. excel.GridLines = GridLines.Both;
17. excel.HeaderStyle.Font.Bold = true;
18. excel.DataSource = dt.DefaultView;//输出DataTable的内容
19. excel.DataBind();
20. excel.RenderControl(htmlWriter);
21.
22. string filestr = "d://data//"+filePath; //filePath是文件的路径
23. int pos = stIndexOf( "//");
24. string file = filestr.Substring(0,pos);
25. if( !Directory.Exists( file ) )
26. {
27. Directory.CreateDirectory(file);
上一篇:红楼梦人物描写风格浅析
下一篇:美国联邦制的发展。