I am trying to capture a spreadsheet data in to a 2D array. I am using VSTO.
int rc = 1048576;
int cc = 1638;
string[,] arr = new string[rc, cc];
Aleks, your are asking for a limit of your memory allocation. Let me suggest to change your point of view and limit your input either
Additionally to your question, let me advise to mind the difference of Range.Value
and Range.Value2
: Range.Value contains the user input e.g. a formula like =SUM(A1:B10)
, while Range.Value2 contains the result of that formula e.g. 10
.
1.1 Using only the user selected cells
As stated here use Range userSelectedRange = Application.Selection;
1.2 Using only the indeed used cells within your xl.worksheet
Use the Worksheet.Cells.SpecialCells
property.
Maybe Worksheet.UsedRange
is even better.
A code example:
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
Let's do the math. You are trying to allocate a 2D string array with 1048576 * 1638 = 1717567488 elements. The size of string reference on x64 is 8 bytes => a total of 1717567488 * 8 = 13740539904 bytes. Which is about 13 GB of continuous memory space. Maximum size for single allocation for CLR is 2GB, so you are getting OutOfMemoryException as such single block can't be allocated.
Note that such amount of strings even when all are 1-2 characters long will take 30GB for string values in addition to references. What else than an OutOfMemoryException
did you expect to get?