I'm writing a program in which I'm using C# language, DataSet
, etc. I have about 200 000 values what I want to export to an .xlsx document.
My code:
using Excel = Microsoft.Office.Interop.Excel;
...
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelworkbook = excelApp.Workbooks.Open(/location/);
Excel._Worksheet excelworkSheet = (Excel.Worksheet)excelApp.ActiveSheet;
...
excelApp.visible = true;
...
for (int i = 0; i < /value/; i++)
for (int j = 0; j < /value/; j++)
excelworkSheet.Cells[i, j] = /value/;
It works well, but it is too slow (at least 5-10 minutes).
Have you got any advice?
For Excel, I only programmed VBA so I cannot give you the exact syntax on how to do it in C#.
What I notice though is that you are doing something that I have noticed many people are tempted to:
Writing code to each cell in Excel separately. Read / Write operations are rather slow in comparison to operations performed in memory.
It would be more interesting to pass an array of data to a function that writes all of these data to a defined range in one step. Before doing so, of course you need to set the dimensions of the range correctly (equal to the size of the array).
However, when doing so, performance should be increased.
I just took the same performance hit, wrote this to benchmark:
Results: