✅ How can I read and edit .XLSX files in C# (WPF)

I am in need of creating a desktop application, where I can see and interact with excel data, cant use database, because it needs to run offline. What are the best libraries I can use? This is my pilot project, starting with C#, I have a good understanding of Excel VBA, if there is something similar to EVBA, that would be helpful. Thanks.
68 Replies
Bladesfist
Bladesfist2y ago
IME they all suck Try closedxml but watch the memory usage, i've seen it use a ridiculous amount of GB to do simple edits to a 10mb file I ended up using an old LGPL version of EP Plus which was much more memory efficient
Devastorian
DevastorianOP2y ago
doesn't closedxml only creates and writes? I would like to also read the file and be able to basically access any cell I desire, or at least read the whole sheet and store it in an Array, which I can parse through and work with it.
Denis
Denis2y ago
I wonder how commercial solutions compare, e.g., devexpress should have something for working with excel files If it can write, it must be able to read?
Bladesfist
Bladesfist2y ago
You can read files with ClosedXML My only issue with it is the insane size of the column and row objects in memory they are absolutely huge
Devastorian
DevastorianOP2y ago
I will have to take a more thorough look. There will be dedicated PC just for that, so It should be fine hopefully.
Angius
Angius2y ago
cant use database, because it needs to run offline
And why should that prevent you from using a database? SQLite and LiteDB exist
Denis
Denis2y ago
Also ms access 💀
Devastorian
DevastorianOP2y ago
Company policy, any kind of a third party software cant use and access server
Bladesfist
Bladesfist2y ago
No 🙂
Angius
Angius2y ago
Uh, Excel is third-party
Bladesfist
Bladesfist2y ago
unless...
Angius
Angius2y ago
Unless you mean SQLite being on the server'
Devastorian
DevastorianOP2y ago
There are a few exceptions, we have a white/black list
Angius
Angius2y ago
In which case... no SQLIte is local
Jimmacle
Jimmacle2y ago
sqlite doesn't even need software installed
Angius
Angius2y ago
^
Jimmacle
Jimmacle2y ago
it's all contained in the library
Devastorian
DevastorianOP2y ago
Also the whole company is working with excel files, so migrating it all would be bigger pain than trying to figure how to parse the excel file
Bladesfist
Bladesfist2y ago
If you can use sqlite, definitely do. It will be a much nicer move than working with xlsx
Angius
Angius2y ago
My condolences Maybe just try using CSV files, dunno
Jimmacle
Jimmacle2y ago
same but my job uses tons of spreadsheets with VBA macros that directly access our DB cringe
Denis
Denis2y ago
harold
Angius
Angius2y ago
Jesus
Bladesfist
Bladesfist2y ago
EPPlus is the best of the ones I've used but the license isn't super friendly
Devastorian
DevastorianOP2y ago
Yep, could not even use things like Selenium to test a few things... I am very limited by what I can do.
Bladesfist
Bladesfist2y ago
Time to look for another company catlaugh
Denis
Denis2y ago
Is it a bank or smth?
Angius
Angius2y ago
Use LiteDB, then. It's an embedded database
Denis
Denis2y ago
Even at commerzbank we don't have such insane regulations
Devastorian
DevastorianOP2y ago
Ohh yeah, I work on these, right now everything is automated by excel files and I am super pissed, it is nightmare to work with, if I need to merge and fix any kind of mistake or added feature
Angius
Angius2y ago
A bank, a school, or a hospital Or a government agency
Jimmacle
Jimmacle2y ago
yeah i'm going to try and move all the logic to an API and reduce the VBA stuff to simple things like requesting data and dumping into the right cells
Devastorian
DevastorianOP2y ago
Worse Corporate for Designing of Catalytic Convertors.
Denis
Denis2y ago
RTXHmm
Devastorian
DevastorianOP2y ago
Basically Automotive, and the higher ups are totally clueless about any kind of programing.
Angius
Angius2y ago
They won't notice you using SQLite then KEKW
Devastorian
DevastorianOP2y ago
Well, I want to stay there for atleast a year to learn how to code in peace, they gave me a lot of time to figure it out. They wont, but our IT team will 😄
Denis
Denis2y ago
Join forces with the it team
Angius
Angius2y ago
Is your IT team just as clueless?
Devastorian
DevastorianOP2y ago
Sadly for me there is no other way but to go Desktop + XLSX as database.
Bladesfist
Bladesfist2y ago
I'd definitely present it as an option alongside using excel and explain why you think it will be better then at least it's on them for shooting you down if it all goes to heck
Devastorian
DevastorianOP2y ago
Naah, they dont really have any power, they are just taking care of the computers etc.. The thing about corporate is, that it takes months and they will on 99% denie it anyway
Angius
Angius2y ago
Worth a shot at least
Denis
Denis2y ago
Higher ups understand money and budget aaand meetings. Setup a meeting, tell them that you need permission from the it team to move forward with the project, otherwise they will have to deepen their pockets in the long term
Angius
Angius2y ago
Especially since it doesn't need any whitelisting or anything Since SQLite is more of a file standard than software
Devastorian
DevastorianOP2y ago
Tried it a few times, with some other things, like if I can use SAP GUI for creating a more user friendly SAP operations, but nope, They care about production, if the project does not yield any added value they dont care, and this just helps maintenance and warehouse management, it is not important for them.
Denis
Denis2y ago
I think getting in touch with it is the first step forward
Devastorian
DevastorianOP2y ago
I am glad they atleast let me install Visual Studio 😅
Denis
Denis2y ago
I once got denied installing vs, because as a developer i pose a huge risk to the bank, as I can change 0's and 1's Wonder why I got hired then...
Jimmacle
Jimmacle2y ago
even as a developer you shouldn't have access to anything that can do any damage
Devastorian
DevastorianOP2y ago
Yeah, that is kinda what is also happening around here, but after a few months there working as the only one who can do some basic sripting and VBA it got better, but not by much.
Jimmacle
Jimmacle2y ago
unless they give devs blanket access to all their systems or something
Denis
Denis2y ago
Exactly, pay me to spin on the shitty office chair
Devastorian
DevastorianOP2y ago
Well, I will try the ClosedXML and see, problem is, that I am talking about thousands of warehouse items, hope it will not be overwhelmed.
Denis
Denis2y ago
And what about... Export to csv, process, and export back to excel....? Does that solve anything?
Bladesfist
Bladesfist2y ago
Hope the thing it's running on has a lot of memory If not you might want to cough up for a EPPlus license
Devastorian
DevastorianOP2y ago
I mean, in terms of posibillity, it is probably doable, but that memory issue might still prevail. Fat chance, but I will see what can be done.
Bladesfist
Bladesfist2y ago
If it's an internal tool you can probably get away with using the old LGPL versions of EP Plus too But that might break dependency rules
Devastorian
DevastorianOP2y ago
parsing the whole sheet into an multidimensional array might be even worse, but that would mean, that I dont have to be connected to that XLSX constantly, just read it once and work with the data
JakenVeina
JakenVeina2y ago
OpenXML is what we use at work, but I've nevr really done, like, any analysis on how good it is, compared to other solutions I do know we can generate spreadsheets with roundabout 300,000 rows before we risk the process running out of memory actually, I think we observed out-of-memory exceptions at like 500,000, and set a limit on ourselves for 300,000
MarkPflug
MarkPflug2y ago
I've benchmarked most of the popular .NET Excel libraries here: https://github.com/MarkPflug/Benchmarks/blob/main/docs/ExcelReaderBenchmarks.md I'm the author of the Sylvan library. If performance and memory efficiency is your main concern, you might give it a try. It provides a minimal feature set to read data out of Excel, but doesn't try to provide the full feature set of other libraries.
GitHub
Benchmarks/docs/ExcelReaderBenchmarks.md at main · MarkPflug/Benchm...
Benchmarks for various .NET libraries. Contribute to MarkPflug/Benchmarks development by creating an account on GitHub.
Bladesfist
Bladesfist2y ago
Nice work, that's really impressive
MarkPflug
MarkPflug2y ago
Thanks. The project repository is here: https://github.com/MarkPflug/sylvan.data.excel
GitHub
GitHub - MarkPflug/Sylvan.Data.Excel: The fastest .NET library for ...
The fastest .NET library for reading Excel data files. - GitHub - MarkPflug/Sylvan.Data.Excel: The fastest .NET library for reading Excel data files.
Devastorian
DevastorianOP2y ago
Thanks, will take a look. I am just trying to do basic stuff, I dont need no styles and fonts etc.. I just want to edit and read values in cells.
JakenVeina
JakenVeina2y ago
definitely gonna bookmark thos for when I get around to re-evaluating our Excel junk at work
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Devastorian
DevastorianOP2y ago
Here is a tutorial, that worked for me. Only problem is, that office needs to be installed for this to work. Also it is needed to close the workbook after being done with the worksheet or else the book will get locked. https://stardevstudio.com/csharp/how-to-read-excel-files-in-csharp/?mwg_rnd=8606258
Darren Grantham
StarDevStudio
How to read Excel files in CSharp - StarDevStudio
We are going to be reading data that is stored in Excel files using CSharp. Writing code to read Excel sheets in C# is really easy!
Devastorian
DevastorianOP2y ago
wb.close(); closes the workbook, it has the same logic and function as programming in excel with VBA.
Want results from more Discord servers?
Add your server