Slow performance when reading Excel with Microsoft.office.Interop.Excel
When reading from excel with Microsoft.office.Interop.Excel
and using, the Range dataRange = (Range)cSheet.Cells[row, col];
performance is very slow. When I delete Range dataRange = (Range)cSheet.Cells[row, col];
it is faster. What I missed. What should I change?
int rows = cSheet.UsedRange.Rows.Count;
int cols = cSheet.UsedRange.Columns.Count;
for (int row = 2; row <= rows; row++)
{
for (int col = 1; col <= cols; col++)
{
Range dataRange = (Range)cSheet.Cells[row, col];
}
}
+3
source to share
1 answer
You want to do it in one operation:
object[,] objectArray = cSheet.get_Range("A1:C4").Value2;
dataRange.Value2 = objectArray;
To get the UsedRange address for "A1: C4" try:
Microsoft.Office.Interop.Excel.Range range = cSheet.UsedRange;
string address = range.get_Address();
string[] cells = address.Split(new char[] {':'});
string beginCell = cells[0].Replace("$", "");
string endCell = cells[1].Replace("$", "");
+4
source to share