简体   繁体   中英

How to Import excel file data to data table in c#

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;
            }
        }

If I understand what you are asking (let me know if not) you just want to be able to filter out rows missing a value in the last column? Best to do an explicit cell reference call rather then trying something like wsRow.Last() because the wsRow range will only return cells that have values in it so the Last() will never return a reference to the last column cell since it would be null.

As for replacing column names, all you need is an if statement when populating the column list.

This should do it:

//foreach (var firstRowCell in ws.Cells[10, 1, 17, ws.Dimension.End.Column])  -- ASSUME YOU MEANT ONLY THE 10TH ROW?
foreach (var firstRowCell in ws.Cells[10, 1, 10, ws.Dimension.End.Column])
{
    if (!hasHeader)
        tbl.Columns.Add(string.Format("Column {0}", firstRowCell.Start.Column));
    else if(firstRowCell.Text == "Description")
        tbl.Columns.Add("Edata");
    else if (firstRowCell.Text == "Column4")
        tbl.Columns.Add("Description");
    else
        tbl.Columns.Add(firstRowCell.Text);
}

var startRow = hasHeader ? 11 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
    //Skip row if last column is null
    if (ws.Cells[rowNum, ws.Dimension.End.Column].Value == null)
        continue;

    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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM