✅ Reading/Writing in Excel with C#
It seems there are plenty of options to read/write .xlsx files (EPPlus, OpenXML, ClosedXML, ...)? Are there some that are "more official" / "better" and that I really should use? Or are they all more or less the same?
27 Replies
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
i've been using closedxml, it has quirks but overall works
but is a huge pain compared to something like csv if you just need to dump data
Don't deal with OpenXML directly for Excel -- it's a huge pain in the ass to work with
I baiscally just need to read values from a .xlsx file
There's NPOI as well, FWIW 😛
I can't make the .xlsx file a .csv file because it is the input of the program that is not controlled by me
I don't need the UI, I really just want to read/write
The Excel might be a few thousands rows but that shouldn't be too much
Eh, working with .xlsx directly is fine, as long as it's not unnecessarily complex
It looks like it's the first one that comes up
OpenXML is the raw .xlsx file format. You're working with XML directly, without any helpers on top. That's OK for word as the object model is fairly straightforward, but excel has a whole bunch of stuff in it which makes working directly with the XML trickly
... which is why wrappers like ClosedXML, EPPluss, exist -- they're there to expose a nice interface to you, and deal with the underlying OpenXML for you
OpenXML recommend the use of ClosedXML, FWIW: https://github.com/dotnet/Open-XML-SDK?tab=readme-ov-file#related-tools
I see, I guess ClosedXML is going to be the one I'll be using
For something simple, as I say it probably doesn't matter. So pick one which has good documentation and a compatible license
for reading you shouldn't have issues, but i'll note that last i checked the stable release of closedxml has problems evaluating certain formulas on save
I had started reading about OpenXML, it seems it uses an unmanaged object that I need to dispose myself, is it standard in C# that I need to be careful about that? Oh was it just OpenXML?
What exactly did you read?
unmanaged or disposable? those are pretty different things
It's standard that if an object implements IDisposable, you need to dispose it (normally with a
using
statement)https://learn.microsoft.com/en-us/office/open-xml/spreadsheet/how-to-open-a-spreadsheet-document-for-read-only-access?tabs=cs-0%2Ccs-1%2Ccs-2%2Ccs
this
sorry I don't think unmanaged was the right term
Which bit of that page?
in that case yes, disposable objects are pretty common
Just the fact that it uses a using
and that therefore I inferred that I would need to be careful about not keeping it in memory
all that means is that you need to call Dispose on it when you're done with it
That's to close the file when you're done
either explicitly or with a using declaration/statement
Ok I see, thanks for the help
I'll dig into the documentation of ClosedXML since it seems to be the most relevant for me
EPPluss seems to not allow commercial use if you don't pay them indeed
Indeed. This stuff can get complex enough that you can end up paying for a good solution
I'm the author of a library for reading Excel data. It has a pretty minimal API, and is very specifically for getting data out of Excel, ie it doesn't handle formatting, styles, charts, etc. It essentially provides a
DbDataReader
over Excel, so is ideal for reading rectangular/tabular data. Though, with a bit of work it can be used to read unusual datasets too. Open source, MIT license, extremely memory/CPU efficient, handles reading .xlsx, .xlsb, and .xls:
https://github.com/MarkPflug/Sylvan.Data.Excel
Might be too minimal for what you need.GitHub
GitHub - MarkPflug/Sylvan.Data.Excel: The fastest .NET library for ...
The fastest .NET library for reading Excel data files. - MarkPflug/Sylvan.Data.Excel
I've ended up going for ClosedXML, I was looking for something fairly standard rather than extremely efficient but thanks for sharing!