I am moving data from excel file to datatable, where 10th row is column values .So i used following code by using EPPLUS library(OfficeOpenXml).When i moved to datatable the Columns are Item,Description,Accountnumber,Tender,Levelnumbers.I said these all are 10th row of the excel file, hence due to merging of top level columns it is coming in a sequence like Item,Column1,Column2,Description,Column3,Column4,Column5,Tender,Column6,Levelnumbers.I need a logic like first i need to skip null rows(no data) for column Levelnumbers then Description column name should be moved to Column4 and current column of Description should be named like 'Edata', so the column sequence should be like Item,Column1,Column2,Edata,Column3,Description,Column5,Tender,Column6,Levelnumbers
So altogether by using following code i got values in data table like
Item,Column1,Column2,Description,Column3,Column4,Column5,Tender,Column6,Levelnumbers
1,null,null,Efax,null,Edescription1,null,Tfirst,null,123353
2,null,null,Zfax,null,Zdescription1,null,Tsecond,null,null
3,null,null,Xfax,null,Xdescription1,null,Tthird,null,456546
But it should come as like(skipped values which has Levelnumbers blank ),how to achieve it?
Item,Column1,Column2,Edata,Column3,Description,Column5,Tender,Column6,Levelnumbers
1,null,null,Efax,null,Edescription1,null,Tfirst,null,123353
3,null,null,Xfax,null,Xdescription1,null,Tthird,null,456546
code used is
public static DataTable getDataTableFromExcel(string path)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
DataTable tbl = new DataTable();
try
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
bool hasHeader = true; // adjust it accordingly( i've mentioned that this is a simple approach)
string ErrorMessage = string.Empty;
foreach (var firstRowCell in ws.Cells[10, 1, 17, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 11 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
tbl.Rows.Add(row);
}
}
catch (Exception exp)
{
}
return tbl;
}
}