问题
Possible Duplicate:
OutofMemory Exception Object Array Size
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];
The last line throws Out of Memory exception. I would like to show message telling the user only 'X' elements can be captured.
Checked MSDN and there is a row count limit mentioned of 16,777,216 for the data-table.So a data table can hold data of size of a worksheet. Cant find limit either for 2D array.
My issue is not with WHY the exception. What I am looking for is if you are doing VSTO development, and had to capture a worksheet in a DataTable to perform In-Memory joins etc, you will need to do this:
string[,] arr = new string[rc, cc];
Microsoft.Office.Interop.Excel.Range selection
arr = selection.Value as string[,];
and then copy the data from that array to data table. Now what will be the ideal limit for number of elements a user should select. So I can set that rowcount/columncount lmits and display message when selection exceeds this criteria. OR is there any other way to create a data table based on selected range in a worksheet, considering first row is always Column headers.
回答1:
What you can do is create your own class:
public class MyClass {
public static string col1 {get;set;}
public static string col2 {get;set;}
public static string col3 {get;set;}
//etc for every column
}
read the first row into the properties of MyClass
then read every row of your worksheet into a list like this:
List<string> myList = new IList<string>();
myList.append(anInstanceofMyClass);
repeat for every row of data
来源:https://stackoverflow.com/questions/12843173/dealing-with-large-selection-ranges-in-excel-interop