Part of our process is to convert several sheets of our created workbook to pdf files. For testing purposes I created a workbook with 10 sheets (all of them have the same content). Sequential conversion is working fine, it's creating the 10 pdf files with one page each as expected. As converting can be rather slow (though still 4.33 times faster than excel interop for my 10 sheets) I tried parallel execution of the conversion. This did not work at all.
Sometimes some of the tasks are null (with the exception "a workbook must contain at least one visible worksheet"), sometimes the tasks run successfully but all returned pdf files contain all 10 sheets, sometimes one of the tasks fails with "'The process cannot access the file '...\ConversionResult8.pdf' because it is being used by another process."
Am I doing something wrong or is parallel conversion not supported?
I know that it may not be worth to run the conversion in parallel but I cannot compare it to sequential execution if it doesn't run successfully.
Here's the code in question:
- Code: Select all
var workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(@"..\..\test.xlsx", ExcelVersion.Version2016);
var conversionTasks = new Task[workbook.Worksheets.Count];
for (var i = 1; i <= workbook.Worksheets.Count; i++)
{
conversionTasks[i - 1] = Task.Factory.StartNew(() => workbook.Worksheets[i].SaveToPdf(@"..\..\ConversionResult" + i + @".pdf"));
}
await Task.WhenAll(conversionTasks);