Ask HN: How would you chunk a large Excel file?
Let's say you had an Excel file with 10,000 rows and you wanted to break it up into many Excel files each with 500 records. Each new file should have the header fields from the original. How would you do it? I did it by writing a node script but I'm wondering if there's an easier way.
Edit: Guys this is just an example. I'm looking for a general solution. It could be 10 million rows.
48 comments
[ 4.4 ms ] story [ 118 ms ] threadIf it had 100k rows, I'd be out of my depth, so I'd hit google.
If you actually had to do it 'properly' there are actually a ton of options:
Not even going to ask AI. Waiting for a bash one-liner before OP reminds us they are on corporate Windows machine.
I dont know the api or recommended scripting language. This would be a good case for chatgpt or equivalent type task. Enough to get started.
edit: I asked chatgpt, it recommended python and 'pandas' for interacting with excel
I asked about the first 'row', and it claims panda includes that in each chunk, but I don't know about that. It's at least a place to start to iterate from. Would need to iterate further with real code/tests.I would strongly consider dumping rows into SQL, for more "natural" selection.
[1] https://learn.microsoft.com/en-us/dotnet/api/microsoft.offic...
For readable/not binary files, there are standard tools like split, awk, etc.
I had a similar problem at work where I needed to do some formula on a 5 GB CSV file. Excel can't handle more than 1M rows. Database through command line is too clunky. I did try to split the CSV into multiple files before but using formula on top of multiple files isn't easy. Eventually I built a Desktop GUI wrapper on SQLite, and it grew into Superintendent.app (now powered by DuckDB).
The newest version supports "Ask AI", which can be used for "Ask your CSVs anything" and "Ask AI to write/rewrite SQLs for you". It has been pretty fun to use and tell AI to "Expand *", "format all dates to something like May 3, 2024", and etc.
If you're worried about data not fitting in memory, then stream the file. It seems like the Java API has support for this, surely other languages do too.
[0] https://xyproblem.info/
Opening and working on an excel file with a few million rows can need a bit more ram than anticipated especially based on it's size/complexity.
The quickest way I'd start with is to convert it to a CSV, read the file in, and rewrite it out 500 lines at a time.
Xlsx or xls?
If it’s xlsx, stream the file, chunking 500 rows into each new file, watching for ref A1, then injecting that into previous files.
The xlsx format is a zip file of xml, not so bad to work with once you start.
I should note, this works best if you don’t care about the order of the rows. They technically aren’t required to be in order (I think).