C # Interop Excel format such as Excel format like table

I am exporting a table from SQLitein Excel(2010) to C#. It is working fine. I am using a method Excel.Range.set_Value().

How can I format the format Excel.Rangeas Excel's(e.g. a table)?

+5
source share
3 answers

Expand my comment and add to D Stanley.

Range range = ws.get_Range("A1:D5");
wrksheet.ListObjects.AddEx(XlListObjectSourceType.xlSrcRange, range, missing, Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing).Name = "MyTableStyle";
wrksheet.ListObjects.get_Item("MyTableStyle").TableStyle = "TableStyleMedium1";
+10
source

In this example, the rectangular range of each cell of the active sheet is selected. In addition, it uses indexed Range parameters to get range points. In addition, AddEx () (and most of the methods in Interop.Excel) uses the default parameters, so you do not need to use System.Reflection.Missing.

// define points for selecting a range
// point 1 is the top, leftmost cell
Excel.Range oRng1 = oSheet.Range["A1"];
// point two is the bottom, rightmost cell
Excel.Range oRng2 = oSheet.Range["A1"].End[Excel.XlDirection.xlToRight]
    .End[Excel.XlDirection.xlDown];

// define the actual range we want to select
oRng = oSheet.Range[oRng1, oRng2];
oRng.Select(); // and select it

// add the range to a formatted table
oRng.Worksheet.ListObjects.AddEx(
    SourceType: Excel.XlListObjectSourceType.xlSrcRange,
    Source: oRng,
    XlListObjectHasHeaders: Excel.XlYesNoGuess.xlYes);
+2

VBA, :

ActiveSheet.ListObjects.Add xlSrcRange, Range("$J$10:$N$12"), , xlYes

. .

-1

All Articles