ExportDataTableAsString Duplicate Column Names

I happen to be attempting to read in a file that has duplicate column names. In this case the file I'm reading happens to have duplicate column names. Using the method ExportDataTableAsString yeilds: System.Data.DuplicateNameException: A column named '[Column Name]' already belongs to this DataTable.

Is there a way to gracefully handle this? I would be content with just appending something like a number to duplicate column names, however the ExportDataTableAsString method doesn't accept any option to do this.

-Damien


Hi Damien,

Thanks for reporting this problem.

Please attach your file and also a code example to help us look into this issue. Please also give a try to latest version of Aspose.Cells for .NET (Latest Version) .

Hi Damien,

How could you have two columns with the same name in a DataTable, this is not possible, so you are getting this error from ExportDataTableAsString() method. I think for your situation you have two options. I have also explained it with examples. Please see the attached template Excel file that does have data but have a similar “Name” for two columns.


1) Please do not export column names when you use ExportDataTableAsString() method, set false for the ExportColumnName parameter.

Sample code:
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(@“e:\test2\import.xlsx”);

Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

//Do not export column names, the columns would come as Column1, Column2 etc.
DataTable dt = worksheet.Cells.ExportDataTableAsString(1, 0, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxDataColumn + 1, false);

Aspose.Cells.Worksheet worksheet2 = workbook.Worksheets[1];

worksheet2.Cells.ImportDataTable(dt, true, “A1”);

workbook.Save(“e:\test2\out1.xlsx”);

2) Please create your desired DataTable with your desired column names, now use appropriate ExportDataTable method instead of ExportDataTableAsString to fill that table.



Sample code:
DataTable dt = new DataTable();
dt.Columns.Add(“ID”, typeof(string));
dt.Columns.Add(“Name”, typeof(string));
dt.Columns.Add(“Class”, typeof(string));
dt.Columns.Add(“Name1”, typeof(string));

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(@“e:\test2\import.xlsx”);

Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

worksheet.Cells.ExportDataTable(dt,1, 0, worksheet.Cells.MaxDataRow + 1, false);


Aspose.Cells.Worksheet worksheet2 = workbook.Worksheets[1];

worksheet2.Cells.ImportDataTable(dt, true, “A1”);

workbook.Save(“e:\test2\out2.xlsx”);

Hopefully, you may adopt any approach for your requirements accordingly.

Thank you.

Thanks for your reply. I solved my problem with a variation of your 2nd solution. I was hoping that I maybe overlooked something in the ExportDataTableAsString method where it would have taken the spreadsheet and automatically appended a number to the end if there were duplicate names.

Thanks again for your help.

-Damien

You are always welcome.

And, good to know that you have sorted it out.

Thank you.