NicolasR
NicolasR

Reputation: 894

EPPlus create same tables in different worksheet

I am trying to create the same tables in two different worksheet but it doesn't seem to work.

I create my tables using this code :

public void createWS(ExcelPackage package)
{
   ExcelWorksheet ws = package.Workbook.Worksheets["WorkSheet1"];
   createTables(ws);
   ws = package.Workbook.Worksheets["WorkSheet2"];
   createTables(ws);
}

public void createTables(ExcelWorksheet ws)
{
   ws.Tables.Add(ws.Cells[1, 1, 301, 1], "Level 1");
}

When I try to create the second one I get the error :

TableName is not unique

Do you have any idea how to create the same table in two different worksheet ?

Upvotes: 1

Views: 3582

Answers (1)

Ernie S
Ernie S

Reputation: 14250

Excel proper (nothing to do with code or EPPlus) requires table names be unique in the same workBOOK. You can try this manually in Excel to see this. So you will have to adjust your function and pass in the table name as a parameter:

public void createTables(ExcelWorksheet ws, string TableName)
{
   ws.Cells[1, 1].Value = "Col1"; //EPPlus generated file will not open properly with this if the cells are all empty
   ws.Tables.Add(ws.Cells[1, 1, 301, 1], TableName);
}

And provide different names.

Upvotes: 2

Related Questions