Same header name will automatically get a count added...?

Posted by: mth on 22 July 2020, 9:49 am EST

  • Posted 22 July 2020, 9:49 am EST

    I have a strange behavior with this code:

    worksheet.Rows[19].Value = new[] {"TableHeaderColumnName", "TableHeaderColumnName"};

    worksheet.Tables.Add(worksheet.Range[$"A{20}:B23"], true);

    In the Excel file generated the value of the second element of the array is changed automatically to: TableHeaderColumnName2

    See screenshot.

    This is a strange behaviour since it's a common scenario to have columns names in a table can be the same and repeat... For example a column named average with a different meaning depending on the previous column...

    Is there a way to prevent this?

  • Replied 22 July 2020, 8:18 pm EST

    Hello Mathieu,

    This is the default behavior of Excel because Excel uses the Table headers in formulas so duplicates cannot use used.
    As a workaround you can put space in the column name to make it look similar, as shown in the code snippet below:

    worksheet.Rows[19].Value = new[] {"TableHeaderColumnName", "TableHeaderColumnName "};

    Prabhat Sharma.
  • Marked as Answer

    Replied 23 July 2020, 1:14 am EST

    I should have thought of that. Thanks!
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels