Import fails with duplicate column names

Oct 12, 2011 at 2:53 PM

Hi!

I have been a long time looking for a solution to import excel files that worked in mono and MS.net. Thanks a lot!

I found a problem that throws an exception when loading Excel files with repeated column names in the same sheet:

The exception shows line 291 of ExcelOpenXmlReader.cs:

  if (_cellsValues[index] != null && _cellsValues[index].ToString().Length > 0)

--> table.Columns.Add(_cellsValues[index].ToString());     --> DuplicateNameException: column "blah blah" already belongs to DataTable

 

Thanks and keep the good work!

Edgar

Oct 12, 2011 at 3:17 PM

Found a solution: Insert a space at the end of the column name.

May be not a good solution, with the user expecting the name to be the same that in the original Excel file...

replace 

table.Columns.Add(_cellsValues[index].ToString()); 

with 

try
{     
    table.Columns.Add(_cellsValues[index].ToString());
}
catch (System.Data.DuplicateNameException)
{
    table.Columns.Add(_cellsValues[index].ToString() + " ");                                
}
Thanks,
Edgar 

Oct 12, 2011 at 3:46 PM

Hmmm... won't work if there are 3 columns with the same name!

Edgar