How to write to excel file faster (writetable, writecell, writematrix or others)
100 views (last 30 days)
Show older comments
I've went through many topics over the years and here is what I found out recently:
- writetable & co are faster than the old xlswrite (or alternatives I found on matlab central)
- writetable & co are still ultra slow when writing to excel. Not to an unbearable level but if the data size is big enough and you have to write tens or hundreds of file (1-100 MB each), you can spend minutes or even hours waiting.
A few tips I found to speed up the write speed to about 4x or 5x is:
- 'AutoFitWidth' = false ...this is true by default and disabling it allowed me to achieve a 2-3x faster write speed (when 'UseExcel' = false, otherwise even more)
- 'UseExcel' = true ...this is false by default and I thought it was a good thing to avoid opening up an Excel instance but you can actually squeeze another 2-3x by switching this to true
A weird interaction is that 'AutoFitWidth' = true is especially punishing when 'UseExcel' = false and this is exactly the default combination. When 'UseExcel' is true, at least in my case, I can get the full 4-5x boost even by leaving AutoFitWidth enabled.
Now, since even with this trick the write speed is still quite low, I was wondering, is there a lower level way to write excel output? Like using fprintf and somehow still saving to excel file? I know it's a bad habit to save to Excel but it still comes in handy when you have to export data for customers, etc. Sharing an Excel file instead of a .txt or whatever usually is usually a big plus in terms of handling for the customer so it would be nice to have a faster way to programmatically write matlab calculated stuff into excel files.
3 Comments
Scott
on 6 Dec 2024
So, I was having similar struggles with creating and writing to an xlsx file. It was taking 30 seconds per file. I originally had UseExcel enabled and Autofitwidth disabled. I turned off useExcel and my performance improved by over 10x to 2.2 seconds! That's on a i9 processor, local M.2 drive, and an xlsx file that was only 40 kb when done. However, I was creating and writing to about 20 seperate sheets in the file and using about 80 writematrix & writecell commands.
Answers (2)
Jan
on 30 May 2021
Thanks for sharing this useful information.
Do I understand correctly that you suggest:
writetabele(data, 'UseExcel', true, 'AutoFitWidth', false)
AutoFitWidth did not exist in R2018b, so it is newer. Some timings on my Windows machine:
x = table(rand(1000, 100));
tic; writetable(x, 'test1.xlsx', 'UseExcel', true); toc
% Elapsed time is 1.795150 seconds. % First call
tic; writetable(x, 'test2.xlsx', 'UseExcel', true); toc
% Elapsed time is 0.667419 seconds. % Second call
tic; writetable(x, 'test3.xlsx', 'UseExcel', false); toc
% Elapsed time is 1.489082 seconds. % Same time for repeated calls
tic; save('test4.mat', 'x'); toc
% Elapsed time is 0.027679 seconds.
The Excel file formats .xls and .xlsx are not efficient in terms of read and write access. There are many binary formats, which outperform the Excel types.
I personally hate Excel and the file formats are one of the reasons for this.
Could you please post some timings with including 'AutoFitWidth', false ?
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!