export Excel to DataTable using NPOI

后端 未结 11 1328
别那么骄傲
别那么骄傲 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:07

    On Codeplex website here in Download section there is example package - a pack of C# examples. Try it, if you haven't yet.

    This is simplest example of it -

    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    
    //.....
    
    private void button1_Click(object sender, EventArgs e)
    {
        HSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(@"c:\test.xls", FileMode.Open, FileAccess.Read))
        {
            hssfwb= new HSSFWorkbook(file);
        }
    
        ISheet sheet = hssfwb.GetSheet("Arkusz1");
        for (int row = 0; row <= sheet.LastRowNum; row++)
        {
            if (sheet.GetRow(row) != null) //null is when the row only contains empty cells 
            {
                MessageBox.Show(string.Format("Row {0} = {1}", row, sheet.GetRow(row).GetCell(0).StringCellValue));
            }
        }
    }  
    

提交回复
热议问题