Exporting the values in List to excel

后端 未结 12 1434
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 18:36

Hi I am having a list container which contains the list of values. I wish to export the list values directly to Excel. Is there any way to do it directly?

12条回答
  •  攒了一身酷
    2020-12-05 19:01

    Using ClosedXML library( there is no need to install MS Excel

    I just write a simple example to show you how you can name the file, the worksheet and select cells:

        var workbook = new XLWorkbook();
        workbook.AddWorksheet("sheetName");
        var ws = workbook.Worksheet("sheetName");
    
        int row = 1;
        foreach (object item in itemList)
        {
            ws.Cell("A" + row.ToString()).Value = item.ToString();
            row++;
        }
    
        workbook.SaveAs("yourExcel.xlsx");
    

    If you prefer you can create a System.Data.DataSet or a System.Data.DataTable with all data and then just add it as a workseet with workbook.AddWorksheet(yourDataset) or workbook.AddWorksheet(yourDataTable);

提交回复
热议问题