使用.NET Core写入Excel

时间:2019-04-18 14:49:28

标签: excel .net-core

使用.net core多年之后,我才刚开始编写我的第一个.net framework控制台应用程序。

在框架中,我编写了许多写给excel文档的应用程序,并且基本上将部分代码粘贴到了核心应用程序中。

private static void createExcelDocument(DataTable dt)
        {
            //creates an instance of excel
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            xlApp = new Excel.Application();
            xlApp.Visible = false;
            xlApp.DisplayAlerts = false;
            object misValue = System.Reflection.Missing.Value;
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            Excel.Worksheet xlWorksheet;
            xlWorksheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorksheet.Name = DateTime.Today.ToString("yyyyMMdd");

            string[] columnHeaders = { "Work Order", "Parent WO", "Description", "Location", "Asset", "Status",
                                       "Scheduled Start", "Scheduled Finish", "Planning Group", "Priority", "Availability?",
                                       "Work Type", "Safety Related Flag", "Parent Location", "Asset Description", "Overdue" };
            int x = 0;
            foreach(string ch in columnHeaders)
            {
                xlWorksheet.Cells[1, x] = ch;
                x++;
            }

            //saves the excel document
            xlWorkBook.SaveAs(folderPath + filename);
            xlWorkBook.Close(true, misValue, misValue);
            cleanUp(xlApp, xlWorkBook, xlWorksheet);
        }

上面的代码确实更改了工作表选项卡的名称,但没有写在列标题中。我想念什么?

1 个答案:

答案 0 :(得分:0)

我会推荐我正在使用的这个程序包,它非常易于使用。 https://github.com/ClosedXML/ClosedXML