在VSTO项目中从Excel工作簿中读取五十万条logging

我正在尝试使用VSTO在Excel中创build一个模拟工具,并创build一个Visual Studio 2010 Office工作簿项目。 本工作手册中的工作表之一将包含大约50万条logging。 理想情况下,我想阅读所有logging在模拟中使用它们,然后输出一些统计数据。 到目前为止,当我试图获得整个范围,然后一个一个去掉它的单元格时,我有OutOfMemoryexception。 有没有人有其他的想法,我怎么能读取所有的数据或build议呢?

这是我的代码:

Excel.Range range = Globals.shData.Range["A2:AX500000"];

Array values = (Array)range.Cells.Value;

如何批量提取,在内存中组装一个内存较less的内存模型?

 var firstRow = 2; var lastRow = 500000; var batchSize = 5000; var batches = Enumerable .Range(0, (int)Math.Ceiling( (lastRow-firstRow) / (double)batchSize )) .Select(x => string.Format( "A{0}:AX{1}", x * batchSize + firstRow, Math.Min((x+1) * batchSize + firstRow - 1, lastRow))) .Select(range => ((Array)Globals.shData.Range[range]).Cells.Value); foreach(var batch in batches) { foreach(var item in batch) { //reencode item into your own object collection. } } 

这不是一个Excel问题,而是一个普通的C#问题。 不是收集内存中的所有行,而是迭代地生成行并计算统计信息。

例如

 class Program { static void Main(string[] args) { var totalOfAllAges = 0D; var rows = new ExcelRows(); //calculate various statistics foreach (var item in rows.GetRow()) { totalOfAllAges += item.Age; } Console.WriteLine("The total of all ages is {0}", totalOfAllAges); } } internal class ExcelRows { private double rowCount = 1500000D; private double rowIndex = 0D; public IEnumerable<ExcelRow> GetRow() { while (rowIndex < rowCount) { rowIndex++; yield return new ExcelRow() { Age = rowIndex }; } } } /// <summary> /// represents the next read gathered by VSTO /// </summary> internal class ExcelRow { public double Age { get; set; } }