Slow Performance When Reading Excel With Microsoft

2019-07-13 11:04发布

When reading from excel with Microsoft.office.Interop.Excel and using Range dataRange = (Range)cSheet.Cells[row, col]; the performance is very slow. When I Remove Range dataRange = (Range)cSheet.Cells[row, col]; it is faster. What did I miss. 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];
  }
}

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-13 11:35

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("$", "");
查看更多
登录 后发表回答