I'm coding on an multithreaded C# Application which provides some statistics for an Excel File. I opened the File by following Code:
private static Excel.Application excelApp = new Excel.Application();
private static Excel.Workbook workbook = excelApp.Workbooks.Open(path);
private static Excel.Worksheet worksheet = workbook.ActiveSheet;
private static Excel.Range range = worksheet.UsedRange;
private static int totalColumns = worksheet.UsedRange.Columns.Count;
To collect the Data out of the columns I'm using one Thread per column.
for (int columnCount = 1; columnCount <= /*range.Columns.Count*/totalColumns; columnCount++)
{
Thread worker = new Thread(printSpread);
worker.Start(columnCount);
}
I got HRESULT: 0x800A01A8 Error if I don't start a new Excel Application in each Thread. My question is, do I have to do this or is there an opportunity to use just one Excel Application? I do think, there is a problem in accessing the data from just one Application by mulitple Threads, which would explain the COM Exception. Thanks for your time and help.
you can read excel file like a single table and save data in memory with a simple datatable. So, you can work in multithread after closing the excel file.
sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test.xls;Extended Properties=""Excel 12.0;HDR=No;IMEX=1"""
using (var conn = new OleDbConnection(sConnection)
{
conn.Open();
string query = "SELECT * FROM [" + mySheetName + "]";
var adapter = new OleDbAdapter(query, conn);
var table = new DataTable();
adapter.Fill(table);
//or you can use datareader
//Now you can close excel and dbConnection
//and work in memory with datatable and threads
}