Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed May 08, 2019 10:20 am

I have a worksheet with a lot - and I mean a lot - of columns, some of which have duplicate names.

When I try to import the data into a DataTable I get an error message 'the column named ["col name"] already belongs to this DataTable.'

Is there a workaround for this, i.e. selecting certain columns?

Please advise.

Thanks

sconly
 
Posts: 12
Joined: Wed May 08, 2019 10:11 am

Thu May 09, 2019 3:23 am

Hello,

Thank you for contacting.
Please kindly note that the column names are used by default as field names of the DataTable when using sheet.ExportDataTable(), but the DataTable can not have the same field names. Thus, you got the error message. However, you can refer to the followng sample code to set the first row of the DataTable does not contain the column names.
Code: Select all
Workbook workbook = new Workbook();       
workbook.LoadFromFile(@"test.xlsx");         
Worksheet sheet = workbook.Worksheets[0];
//specify the used range of the sheet
CellRange range = sheet.Range[sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn];
//the "false" is setting the first row of the DataTable does not contain the column names
DataTable dataTable = sheet.ExportDataTable(range, false, true);

Or, you can use the following code snippet to select certain columns without setting the first row of the DataTable does not contain the column names.
Code: Select all
......
Worksheet sheet = workbook.Worksheets[0];
//select the first and second columns
CellRange range = sheet.Range["A1:B5"];
//the first "true" is setting the first row of the DataTable contains the column names
DataTable dt = sheet.ExportDataTable(range, true, true);

If there is any question, please provide your testing file to help us further look into it, you could send it to us via email(support@e-iceblue.com).

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.XLS