C
C#2mo ago
Jasonnn

✅ 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
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle2mo ago
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
canton7
canton72mo ago
Don't deal with OpenXML directly for Excel -- it's a huge pain in the ass to work with
Jasonnn
Jasonnn2mo ago
I baiscally just need to read values from a .xlsx file
canton7
canton72mo ago
There's NPOI as well, FWIW 😛
Jasonnn
Jasonnn2mo ago
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
canton7
canton72mo ago
Eh, working with .xlsx directly is fine, as long as it's not unnecessarily complex
Jasonnn
Jasonnn2mo ago
It looks like it's the first one that comes up
canton7
canton72mo ago
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
Jasonnn
Jasonnn2mo ago
I see, I guess ClosedXML is going to be the one I'll be using
canton7
canton72mo ago
For something simple, as I say it probably doesn't matter. So pick one which has good documentation and a compatible license
Jimmacle
Jimmacle2mo ago
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
Jasonnn
Jasonnn2mo ago
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?
canton7
canton72mo ago
What exactly did you read?
Jimmacle
Jimmacle2mo ago
unmanaged or disposable? those are pretty different things
canton7
canton72mo ago
It's standard that if an object implements IDisposable, you need to dispose it (normally with a using statement)
canton7
canton72mo ago
Which bit of that page?
Jimmacle
Jimmacle2mo ago
in that case yes, disposable objects are pretty common
Jasonnn
Jasonnn2mo ago
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
Jimmacle
Jimmacle2mo ago
all that means is that you need to call Dispose on it when you're done with it
canton7
canton72mo ago
That's to close the file when you're done
Jimmacle
Jimmacle2mo ago
either explicitly or with a using declaration/statement
Jasonnn
Jasonnn2mo ago
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
canton7
canton72mo ago
Indeed. This stuff can get complex enough that you can end up paying for a good solution
MarkPflug
MarkPflug2mo ago
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
Jasonnn
Jasonnn2mo ago
I've ended up going for ClosedXML, I was looking for something fairly standard rather than extremely efficient but thanks for sharing!
Want results from more Discord servers?
Add your server