Export to DataTable - First Row as Column Headers

Good Morning,

I am currently evaluating ASPOSE.cells to see if it is a viable solution for our .NET needs. I have successfully exported(imported) an Excel spreadsheet into a DataTable, and then DataSet, but I have one issue. I cannot find a directive to have the first row of the DataTable be the actual column names. When using MSJet or ACE drivers, you can use HDR=Yes to accomplish this, and the ExcelDataReader on CodePlex offers IsFirstRowAsColumnNames = True. Is there a way to accomplish this in ASPOSE?

Thank You,

Kevin Gordon

Hi Kevin,


Thank you for considering Aspose products.

You may use the ExportTableOptions.ExportColumnName property to assign the columns header as column names in the exported DataTable. Please check the following code snippet for better elaboration.

C#

var book = new Workbook(“D:/temp/book1.xlsx”);
var sheet = book.Worksheets[0];
var cells = sheet.Cells;
var table = cells.ExportDataTable(0, 0, cells.MaxDataRow, cells.MaxDataColumn + 1, new ExportTableOptions() { ExportColumnName = true});

Please feel free to write back in case you face any difficulty.
1 Like

Thanks. That worked perfectly.

Thank You,

Kevin Gordon

Hi Kevin,


It’s good to know that the provided solution helped with your requirement. Please feel free to write back in case you need our further assistance with Aspose APIs.

Hi Babar,

In case i change the first row other than zero than column names are not set as expected, please see the below highlighted code -

var table = cells.ExportDataTable(10, 0, cells.MaxDataRow, cells.MaxDataColumn + 1, new ExportTableOptions() { ExportColumnName = true});

@kundanpandey,

Well, in ExportDataTable method, the first row will always be taken as column names when you specify ExportColumnName Boolean parameter to true. This is common scenario as column names will always be the top row for a certain dataset. So, your code will not work as per your needs if you want to accomplish your custom oriented requirements (which is not general) where the column names won’t come from very first record (10th indexed position in your case).

1 Like

Thanks Amjad… got a workaround for this.

@kundanpandey,

You are welcome.