I have a C# add-in for Excel and I need to put the data on a worksheet. I do it like so:
// Now build a string array of the results
string[,] resultArray = new string[objects.Length, length];
// Fill in the values
Excel.Range resultRange = worksheet.get_Range("A2").get_Resize(objects.Length, length);
resultRange.Value = resultArray;
I have left out some unimportant steps. Basically, I am passed an array of objects, I get the type from the first one and use the properties to build a list of column names. I put that in the row 1. That is why I start the data in row 2.
The issue I ran across is that I had an Excel 97-2003 workbook (with max rows of around 65K row) and I tried to bring in 105K objects. It choked on the resize. I would like to check to see if my resize is even valid so I can tell the user, but I can’t seem to find a “MaxRows” or similar property. Is there one?
worksheet.Rows.Countwill give you the maximum number of rows in a given worksheet. If you check this property before accessing a large number of rows you can make your program compatible with 2003, 2007, and take advantage of increased numbers of rows in all future versions of Excel.