export Excel to DataTable using NPOI

后端 未结 11 1299
别那么骄傲
别那么骄傲 2021-02-19 03:22

I want to read Excel Tables 2010 xlsx using NPOI and then export data to DataTables but don\'t know how to use it. Can anyone show me step by step how to export Excel to Datatab

11条回答
  •  抹茶落季
    2021-02-19 04:16

    You can accomplish you task by doing this.

    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using NPOI.Util.Collections;
    using NPOI;
    using System.Collections.Generic;
    using NPOI.OpenXmlFormats.Spreadsheet;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.Util;
    
    public DataTable xlsxToDT(Stream str)
    {
    XSSFWorkbook hssfworkbook = new XSSFWorkbook(str);
    ISheet sheet = hssfworkbook.GetSheetAt(0);
    str.Close();
    
    DataTable dt = new DataTable();
    IRow headerRow = sheet.GetRow(0);
    IEnumerator rows = sheet.GetRowEnumerator();
    
    int colCount = headerRow.LastCellNum;
    int rowCount = sheet.LastRowNum;
    
    for (int c = 0; c < colCount; c++)
        dt.Columns.Add(headerRow.GetCell(c).ToString());
    
    while (rows.MoveNext())
    {
        IRow row = (XSSFRow)rows.Current;
        DataRow dr = dt.NewRow();
    
        for (int i = 0; i < colCount; i++)
        {
            ICell cell = row.GetCell(i);
    
            if (cell != null)
                dr[i] = cell.ToString();
        }
        dt.Rows.Add(dr);
    }
    return dt;
    

    }

提交回复
热议问题