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_Res开发者_开发问答ize(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.Count
will 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.
Here are the hard limits for a worksheet in Excel 2003. There is no programmatic way to exceed these limits, although you could start spreading data across multiple sheets or various other bits of trickery.
Excel 2010 has expanded these limits significantly. So an option might be to get your customer to upgrade as a part of the project.
精彩评论