How to read from XLSX (Excel)?

后端 未结 2 604
忘了有多久
忘了有多久 2020-12-15 17:31

I have a problem with reading from .xlsx (Excel) file. I tried to use:

var fileName = @\"C:\\automated_testing\\ProductsUploadTemplate-2015-10-22.xlsx\";
var         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 17:58

    Reading Excel files with OLE provider is possible only if MS Jet engine (MS Access) is installed. I noticed that you decided to use .NET interop to API but this is not a good idea: it requires installed MS Excel and doesn't recommended to use for automation on servers.

    If you don't need to support old (binary) Excel formats (xls) and reading XLSX is enough I recommend to use EPPlus library. It provides simple and powerful API for both reading and writing XLSX files (and has a lot of examples):

    var existingFile = new FileInfo(filePath);
    // Open and read the XlSX file.
    using (var package = new ExcelPackage(existingFile)) {
       // access worksheets, cells etc
    }
    

提交回复
热议问题