How to export DataTable to Excel

后端 未结 21 2308
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 15:36

How can I export a DataTable to Excel in C#? I am using Windows Forms. The DataTable is associated with a DataGridView control. I have

21条回答
  •  隐瞒了意图╮
    2020-11-22 15:51

    You can use EasyXLS that is a library for exporting Excel files.

    Check this code:

    DataSet ds = new DataSet();
    ds.Tables.Add(dataTable);
    
    ExcelDocument xls = new ExcelDocument();
    xls.easy_WriteXLSFile_FromDataSet("datatable.xls", ds, 
               new ExcelAutoFormat(Styles.AUTOFORMAT_EASYXLS1), "DataTable");
    

    See also this sample about how to export datatable to excel in C#.

提交回复
热议问题