使用Microsoft.office.Interop.Excel读取Excel时性能降低 [英] Slow Performance When Reading Excel With Microsoft.office.Interop.Excel

查看:75
本文介绍了使用Microsoft.office.Interop.Excel读取Excel时性能降低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 Microsoft.office.Interop.Excel 从excel读取并使用 Range dataRange =(Range)cSheet.Cells [row,col]; 时,性能非常好慢的.当我删除 Range dataRange =(Range)cSheet.Cells [row,col]; 时,它会更快.我错过了什么.我应该改变什么?

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];
  }
}

推荐答案

您要执行一次操作:

object[,] objectArray = cSheet.get_Range("A1:C4").Value2;
dataRange.Value2 = objectArray;

要获取"A1:C4"的UsedRange地址,请尝试:

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("$", "");

这篇关于使用Microsoft.office.Interop.Excel读取Excel时性能降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆