This project has moved and is read-only. For the latest updates, please go here.

How to get Column name from a table in excel file

Jun 28, 2011 at 7:50 AM

How to get Column name from a table in excel file.

I have imported a excel file (2010) using ClosedXML, but I'm unable to get column name of that table, only table data is retrieving. Please help me.

Jun 28, 2011 at 3:09 PM
                foreach (var table in ws.Tables)
                {
                    Console.WriteLine(table.Name);
                }

Jun 28, 2011 at 3:51 PM

No, this gives the names of the tables in the given worksheet and not the names of the columns in a table. However, I was able to find a solution to get the column names by 

var workbook = new XLWorkbook(filePath);
var xlWorksheet = workbook.Worksheet(1);
var xlTable = xlWorksheet.Tables.FirstOrDefault();
IXLRangeRow xlRangeRow = xlTable.HeadersRow();

var datatable = new DataTable();
foreach (var cell in xlRangeRow.Cells())
{
      datatable.Columns.Add(cell.Value.ToString());
}

Jun 28, 2011 at 3:55 PM

Sorry about that,

dataTable.Field(0).Name