WRITING AN EXCEL TABLE TO EXCEL USING INTEROP

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
C# 
C#
 1159  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. Pravesh Singh

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!