手机版

C中数据导出到Excel表格(12)

时间:2025-07-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);

C中数据导出到Excel表格(12).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)