I am working on some code in which I have to open excels, change some cells values and then re-calculate the formulas of the whole file in order to get two specific values and inform them.
The problem is that I have to do this with a high number of files, all with different names and in different paths, and is taking too much time to create the workbook each time.
Even working with only one excel takes a few seconds to process, and the big stop is in the creation of the workbook object.
Is there some way to speed up this process?
This is my code:
class Program
{
private class MyWorkbook : IDisposable
{
public Workbook Wb { get; set; }
public MyWorkbook(Workbook wb)
{
Wb = wb;
}
public void Dispose()
{
}
}
static void Main(string[] args)
{
Console.WriteLine("Started: " + DateTime.Now.ToString());
int i = 0;
while (i <= 10000)
{
using (MyWorkbook workbook = new MyWorkbook(new Workbook(@"C:\AppData\MyExcels\abcdefg.xlsm")))
{
workbook.Wb.Settings.CreateCalcChain = false;
workbook.Wb.Worksheets[1].Cells["M21"].Value = 5;
workbook.Wb.Worksheets[1].Cells["M22"].Value = 5;
workbook.Wb.Worksheets[1].Cells["M23"].Value = 5;
workbook.Wb.CalculateFormula();
Console.WriteLine(workbook.Wb.Worksheets[1].Cells["L81"].Value);
}
Console.WriteLine("Index: " + i);
i++;
}
Console.WriteLine("Finished: " + DateTime.Now.ToString());
Console.ReadLine();
}
}
Thank you.