I am working on an application that needs to edit an excel file performing various actions like:
- Copy paste sections from worksheet to worksheet
- Add/Edit values
- Formulas and macros run in the background
This is an application initially developed with Excel 2007 but I now have to use Excel 2013. Unfortunately, I noticed that it is almost 10x times slower when running in a machine that has Excel 2013 installed rather than Excel 2007.
Therefore, I created a very simple benchmarking application that randomly generates numbers and writes them in 1000 (rows) * 15 (columns) = 15000 cells.
The results are disappointing as it takes around 670 milliseconds to run in Excel 2007 and around 3257 milliseconds to run in Excel 2013. (this is 5x times slower)
Please find the code that gives the above benchmark results:
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Reset();
stopwatch.Start();
var excel = new Application();
excel.Visible = false;
excel.DisplayAlerts = false;
var workbooks = excel.Workbooks;
var workbook = workbooks.Add(Type.Missing);
var worksheets = workbook.Sheets;
var worksheet = (Worksheet)worksheets[1];
WriteArray2(1000, 15, worksheet);
workbook.SaveAs(@"c:\temp\Speedtest.xlsx");
excel.Quit();
stopwatch.Stop();
Console.WriteLine("Time elapsed (milliseconds): "+(stopwatch.ElapsedMilliseconds).ToString());
Console.ReadLine();
return;
}
private static void WriteArray2(int rows, int columns, Worksheet worksheet)
{
var data = new object[rows, columns];
Random r = new Random();
for (var row = 1; row <= rows; row++)
{
for (var column = 1; column <= columns; column++)
{
data[row - 1, column - 1] = r.NextDouble()*2000000-1000000;
}
}
var startCell = (Range)worksheet.Cells[1, 1];
var endCell = (Range)worksheet.Cells[rows, columns];
var writeRange = worksheet.Range[startCell, endCell];
writeRange.Value2 = data;
}
}
Have you had any similar issues with Excel 2013? Do you know if there is anything that I can try to optimise Excel 2013?
I know that some people have raised performance issues in Excel 2013 but Microsoft has not admitted that as being a fact.
Thanks for your help!