Writing an Excel table to Excel using Interop

Total Post:149

Points:1043
C# 
C#
 1410  View(s)
Ratings:
Rate this:

I am using Excel Interop to generate Excel Table into an xlsx file. I have a very simple table with 3 columns and no header, no totals but has 2 rows. Now when I generate the Excel file, I use the following code

Range rng = worksheet.Range["$A$1:$C$2"];

ListObject lo = worksheet.ListObjects.Add(xlSrcRange, rng, Type.Missing, XlYesNoGuess.xlNo);

lo.ShowHeaders = false;

With the above code it generates the table with cell range A2:C3 instead of A1:C2. But any values I set in the A row are set but outside of the table. If I set the ShowHeaders to true in the 3rd line of code above, then Excel is converting the top row as the header line and still moves the table cell range to A2:C3

Am I doing something wrong? I appreciate any and all help and guidance to fix this issue.

Thanks

  1. Post:412

    Points:2888
    Re: Writing an Excel table to Excel using Interop

    Hi Royce,


    Try this

    Range rng = worksheet.Range["$A$1:$C$2"];

    ListObject lo = worksheet.ListObjects.Add(xlSrcRange, rng, Type.Missing, XlYesNoGuess.xlNo);

    lo.ShowHeaders = false;

    Excel.Range rngRowOne = worksheet.get_Range("A1", "A1");

    rngRowOne.EntireRow.Delete(Excel.XlDirection.xlUp);

Answer