在Excel工作簿中将数据读取到ArrayList中时,性能低下

这是一个问题描述。

条件:一般的想法是从MS Excel文件中读取大量实数,并将它们放入ArrayList进一步处理。 一个Excel工作簿只有一个工作表。 所有的数字是真实的,他们被存储在一列。 我逐行读取这些数字,并将它们放入ArrayList中。

问题:这个过程花费太多时间。 程序花费大约2分钟来填充10000个元素的ArrayList。 这是我的代码。 我需要你的build议,使其更快。 但是文件的结构不能修改。 只能修改代码。 请帮助我,让它更快。

// Method GetExcelData opens 1 excel file, reads data row by row and adds // it into the array of source Data Values (sourceDataValues in our case). private void GetExcelData(string fullPath, ArrayList arrForValues) { Excel.Application excelapp = new Excel.Application(); excelapp.Visible = false; // to avoid appearing of Excel window on the screen Excel.Workbook excelappworkbook = excelapp.Workbooks.Open( fullPath, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet excelworksheet = (Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1); Excel.Range excelcells = excelworksheet.UsedRange; uint rowsNum = 0; for (rowsNum = 1; rowsNum != excelcells.Rows.Count; rowsNum++) { arrForValues.Add((excelcells.Cells[rowsNum, 1] as Excel.Range).Value2); } excelappworkbook.Close(false, Type.Missing, Type.Missing); excelapp.Quit(); } 

问题解决了。 一切都很简单。 首先,我们将当前工作表的所有范围读入简单的二维数组 – worksheetValuesArray。 之后,我们把这个数组的所有值放到我们的容器中,把元素的types转换成double。 以下是更正的解决scheme的一部分:

 private void GetExcelData(string fullPath, List<double> arrForValues) { Excel.Application excelapp = new Excel.Application(); excelapp.Visible = false; // to avoid appearing of Excel window on the screen Excel.Workbook excelappworkbook = excelapp.Workbooks.Open( fullPath, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet excelworksheet = (Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1); Excel.Range excelcells = excelworksheet.UsedRange; object[,] worksheetValuesArray = excelcells.get_Value(Type.Missing); for (int col = 1; col < (worksheetValuesArray.GetLength(1)+1); col++) { for (int row = 1; row < (worksheetValuesArray.GetLength(0)+1); row++) { arrForValues.Add((double) worksheetValuesArray[row, col]); } } excelappworkbook.Close(false, Type.Missing, Type.Missing); excelapp.Quit(); } 

我对Excel自动化的经验是总是很慢。 我通常尝试一种替代方法,例如将其保存为CSV,并使用stream读取器读取数据,并将string拆分为分隔符(逗号,制表符等)。 我会build议看看接收你的数据的过程,看看是否有另一种格式随时可用。

我调整了for循环。 看看这是否会产生更好的结果。

  // Method GetExcelData opens 1 excel file, reads data row by row and adds // it into the array of source Data Values (sourceDataValues in our case). private void GetExcelData(string fullPath, ArrayList arrForValues) { Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application(); excelapp.Visible = false; // to avoid appearing of Excel window on the screen Microsoft.Office.Interop.Excel.Workbook excelappworkbook = excelapp.Workbooks.Open( fullPath, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet excelworksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range excelcells = excelworksheet.UsedRange; Microsoft.Office.Interop.Excel.Range newRange = excelworksheet.get_Range("A1","A"+excelcells.Rows.Count); object[,] items = newRange.Value; for (int i = 1; i < items.Length; i++) { arrForValues.Add(items[i,1]); } excelappworkbook.Close(false, Type.Missing, Type.Missing); excelapp.Quit(); } 

我不知道你是否会find更多的performance。 Excel interop只是缓慢(由于我假设跨COM边界封送)。 通过设置以下内容,我在代码中获得了一些性能(特别是在Excel 2007及更高版本中)。

excelapp.ScreenUpdating = false;

excelapp.Calculation = Excel.XlCalculation.xlCalculationManual;