Domanda

I m using OfficeOpenXML to import Excel data into Database. here is the code sample that I am using for my task.But I want to have HeaderName while looping through each cell in row As I have to make some modifications to columns. Any help suggestion appreciated. Thanks!

 var ws = pck.Workbook.Worksheets.First();
 var pck = new OfficeOpenXml.ExcelPackage();
for (var rowNum = 3; rowNum <= ws.Dimension.End.Row; rowNum++)
    {
      var wsRow = ws.Cells[rowNum, 1, rowNum, DtCommon.Tables[0].Columns.Count-1];
      var row = tblResult.NewRow();
       foreach (var cell in wsRow)
          {
             row[cell.Start.Column - 1] = cell.Text; //Find HeaderName here based on cell index
           }
      tblResult.Rows.Add(row);
    }
È stato utile?

Soluzione

I've posted this suggestion in the comments, but as it helped, I'm posting it as an answer.

By looking at Your code, I suppose You're using ExcelPackage library.

If so, I suppose You may be able to get the first cell in each column by writing:

foreach (var cell in wsRow)
{
    // code to get the header cell
    var header = ws.Cell(1, cell.Column);

    // rest of your code here
    // ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top