zoukankan      html  css  js  c++  java
  • NPOI读取excel文件

    获取excel里面的数据 放进datatable

    if (this.fuUpload.HasFile)
    {

       //根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
        HSSFWorkbook workbook = new HSSFWorkbook(this.fuUpload.FileContent);

       //获取excel的第一个sheet
        HSSFSheet sheet = workbook.GetSheetAt(0);
     
        DataTable table = new DataTable();
       //获取sheet的首行
        HSSFRow headerRow = sheet.GetRow(0);

       //一行最后一个方格的编号 即总的列数
        int cellCount = headerRow.LastCellNum;
     
        for (int i = headerRow.FirstCellNum; i < cellCount; i++)
        {
            DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
            table.Columns.Add(column);
        }
       //最后一列的标号  即总的行数
        int rowCount = sheet.LastRowNum;
     
        for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++)
        {
            HSSFRow row = sheet.GetRow(i);
            DataRow dataRow = table.NewRow();
     
            for (int j = row.FirstCellNum; j < cellCount; j++)
            {
                if (row.GetCell(j) != null)
                    dataRow[j] = row.GetCell(j).ToString();
            }
     
            table.Rows.Add(dataRow);
        }
     
        workbook = null;
        sheet = null;
     
        this.gvExcel.DataSource = table;
        this.gvExcel.DataBind();
    }

  • 相关阅读:
    Git在eclipse中的使用
    Git协同开发产生的版本冲突
    git&github-远程库的拉取
    【题解】p6160 [Cnoi2020]向量
    【题解】p2388 阶乘之乘
    友情链接
    O(1)求解自然数异或和
    【题解】uva1104 chips challenge
    【题解】p1809 过河问题
    多步操作产生错误,请检查每一步的状态
  • 原文地址:https://www.cnblogs.com/linzheng/p/1912137.html
Copyright © 2011-2022 走看看