C# - convert xls file to xlsx without office components

后端 未结 2 1570
臣服心动
臣服心动 2020-12-15 13:53

I have a server that can\'t have any office installed on it, so I\'m using ClosedXML to do some manipulations on excel files. But ClosedXML only works on .xlsx file and I al

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 14:09

    For Microsoft.Office.Interop you need to have office installed.

    You could use OleDb to read it(f.e. into a DataTable). Then use a library like EPPlus or OpenXml to write the xlsx file. Both don't need to have office installed.

    Creating an xlsx file from a DataTable with EPPLus is easy:

    using (ExcelPackage pck = new ExcelPackage(newFile))
    {
      ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Name of Worksheet");
      ws.Cells["A1"].LoadFromDataTable(dataTable, true);
      pck.Save();
    }
    

    The first link above shows how to get a DataTable from an xls-file.

提交回复
热议问题