Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, February 3, 2010 5:57 PM
In my C# program, I'm creating an excel file (using Microsoft.Office.Interop.Excel). In this excel file, I'd like to be able to create a table over a range of data.
Basically, I want to mimic what I can do in excel. For example, in Excel, I select a range of cells (A5-C10) and click Insert->Table. A "Create Table" dialog appears, I check that my table has headers and click ok. Now my headers act as filters where I can sort the rows.
How can I do this from C#?
Thanks,
Brandon
All replies (2)
Wednesday, February 3, 2010 8:31 PM âś…Answered | 1 vote
I don't necessarily like this, but it does work.
Code Snippet
- public static void SetTable(String FileName)
- {
- xls.Application ExcelObj = new xls.Application();
- ExcelObj.DisplayAlerts = false;
- ExcelObj.Visible = true;
- xls.Workbook eBook = ExcelObj.Workbooks.Open(FileName, 2, false,
- 5, "", "", true, xls.XlPlatform.xlWindows, "",
- false, false, 0, false, true, 0);
- xls.Worksheet ws = (xls.Worksheet)eBook.Worksheets["Sheet1"];
- xls.Range tRange = ws.get_Range("A1", "C4");
- ws.ListObjects.Add(xls.XlListObjectSourceType.xlSrcRange, tRange,
- Type.Missing, xls.XlYesNoGuess.xlYes, Type.Missing).Name = "TestTable";
- ws.ListObjects["TestTable"].TableStyle = "TableStyleMedium3";
- //ExcelObj.Workbooks.Close();
- //ExcelObj.Quit();
- }
Thursday, February 4, 2010 5:12 PM
Thanks, that works great!