Home » excel » Easily read Excel in C#?

Easily read Excel in C#?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve tried the OleDb driver, LinqToExcel, and Excel Data Reader to read .xls files, but all of them seem to have very annoying limitations. LinqToExcel and the OleDb driver both throw “Too Many Fields Defined” error messages if the excel files have phantom columns. The Excel Data Reader threw undefined exceptions, which I was never able to get to the bottom of.

Is there any excel driver that “just works”, and can handle slightly mis-formatted excel files?

A commercial software package would be fine. My current requirements only specify reading dates and text from cells, though more sophisticated functionality would be a plus.

[Edit]

Needs to support both XLS and XLSX file formats.

How to&Answers:

I can recommend Aspose.Cells and Flexcel… didn’t try SpreadsheetGear but hear+read lots of good things about it…

A free option (though for the newer xlsx format only!) is OpenXML 2 from MS.

Answer:

Try Epplus Open Source library for excel

Answer:

Even if i did not yet try, this project seems interesting. It is minded expecially for writing but it just work even for read. Unfortunately it accepts the new xslx formats.

Answer:

I’ve used SpreadSheetGear previously. You have to pay but it worked very well for my needs and handled the different file formats well.

I tested a few other libs but SSG worked best in terms of maintaining fidelity of the file when I saved copies of it but then my files had lots of data validations and controls in place etc. For simpler files there’s a range of other options.

Answer:

If all else fails you could write an Excel macro and, if needed, call the macro from within C#. I am not sure if you need that as you do not give any reasons why you are doing this in C#

Another way into the data is by using an interop assembly which has become much easier since the arrival of the dynamic keyword in C# 4