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.
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) .
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”);
//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);
2) Please create your desired DataTable with your desired column names, now use appropriate ExportDataTable method instead of ExportDataTableAsString to fill that table.
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.