Many times we need to insert text to a contiguous Excel range using VSTO (like column headings). The following code does exactly that
private void InsertHeading()
{
string[] Header = { "Sno", "Product Code", "Product Name", "Quanity", "Price", "Total" };
Excel.Worksheet sht = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
Excel.Range myHeader = sht.get_Range("A1", "F1");
myHeader.Value2 = Header;
myHeader.Columns.AutoFit();
}
That works well for column headers. What if you wanted to populate a contiuguous range of rows in that fashion? If you use the method above and simply change the range A1:F1 to A1:A6, you will populate each cell with the first element of your C# array. Is there a way to generate row headers without resorting to a for loop and doing it element-by-element?
ReplyDeleteCreate a two dimensional array but only fill one dimension,
ReplyDelete