OutofMemoryexception对象数组大小

我正在尝试将电子表格数据捕获到2D数组中。 我正在使用VSTO。

int rc = 1048576; int cc = 1638; string[,] arr = new string[rc, cc]; 

最后一行抛出Out of Memoryexception。 我想显示消息告诉用户只能捕获’X’元素。

检查MSDN并提到了16,777,216的行计数限制。 无数据表的列数限制。 无法找到2Darrays的限制。

我的问题不是为什么例外。 我正在寻找的是,如果您正在进行VSTO开发,并且必须捕获DataTable中的工作表以执行内存中连接等,您将需要这样做:

 string[,] arr = new string[rc, cc]; Microsoft.Office.Interop.Excel.Range selection arr = selection.Value as string[,]; 

然后将该数组中的数据复制到datatable。 现在,用户应该选择的元素数量的理想限制是什么。 因此,当选择超出此条件时,我可以设置rowcount / columncount lmits并显示消息。

我们来做数学。 您正在尝试分配具有1048576 * 1638 = 1717567488元素的2D字符串数组。 x64上字符串引用的大小为8个字节=>总共1717567488 * 8 = 13740539904个字节。 这大约是13 GB的连续内存空间。 CLR的单个分配的最大大小为2GB,因此您将获得OutOfMemoryException,因为无法分配单个块。

请注意,即使长度都是1-2个字符,这样的字符串数量除了引用之外还需要30GB的字符串值。 你期望得到什么比OutOfMemoryException更好?

Aleks,你要求限制内存分配。 我建议改变你的观点并限制你的输入

  • 1.1仅使用用户选择的单元格
  • 1.2仅使用xl.worksheet中确实使用过的单元格

除了你的问题,让我建议记住Range.ValueRange.Value2 :Range.Value包含用户输入,例如类似=SUM(A1:B10)的公式,而Range.Value2包含该公式的结果例如10

1.1仅使用用户选择的单元格
如上所述,使用Range userSelectedRange = Application.Selection;

1.2仅使用xl.worksheet中确实使用的单元格
使用Worksheet.Cells.SpecialCells属性。
也许Worksheet.UsedRange更好。

一个代码示例:

 int lastUsedRowIndex = xlWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Row; int lastUsedColIndex = xlWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Column; string rangeString = string.Format("A0:{0}{1}", SomeIndexToExcelColConverterFunc(lastUsedColIndex), lastUsedRowIndex)); Xl.Range range = xlWorksheet.Range[rangeString]; object[,] objectMatrix = range.Value2 as object[,]; Marshal.ReleaseComObject(range); // convert to string here if needed