当前位置:编程学习 > C#/ASP.NET >>

菜鸟 读取Excel数据到ext.net的Gridpanel里 读取的时候就报错

报错:Unable to Read entire header; 0 bytes Read; expected 512 bytes 
行 179:            HSSFWorkbook workbook = new HSSFWorkbook(excelFileStream);
EXCEL数据
物资名称  长度 宽度 高度  备注

cs代码

  protected void ImpExcel(object sender ,DirectEventArgs e)
        {
               //获取导入的excel的datatable
            DataTable dt = CommonUtilies.ImportDataTableFromExcel(FileUploadField2.PostedFile.InputStream, 0, 0);
            DataTable tempDt = dt.Clone();
            tempDt.Columns.Add("ID");
            tempDt.Columns.Add("GOODSNAME");
            tempDt.Columns.Add("GOODSLONG");
            tempDt.Columns.Add("GOODSWIDTH");
            tempDt.Columns.Add("GOODSHEIGHT");
            tempDt.Columns.Add("REMARK");
            int count = 0;
            String message = "";
            foreach (DataRow row in dt.Rows)
            {
                DataRow newRow = tempDt.NewRow();
                    //物资dt
                DataTable goodsDt = new pgms.BLL.GoodsBLL().GetAllGoods();
                    bool islong = CommonUtilies.isnum (row["长度"].ToString ());
                    bool iswidth = CommonUtilies.isnum (row["宽度"].ToString ());
                    bool isheight = CommonUtilies.isnum (row["高度"].ToString ());
                    string goodsname = string.Empty;
                    
                        foreach (DataRow grow in goodsDt.Rows)
                        {
                            if (row["物资名称"].ToString().Trim() == grow["goodsname"].ToString().Trim())
                            {
                                goodsname = grow["goodsname"].ToString();
                                newRow["物资名称"] = goodsname;

                                break;
                            }
                        }
                        if (!string.IsNullOrEmpty(goodsname)&&islong &&iswidth &&isheight )
                        { 
                               count++;   
                        }
                        else
                        {
                            message += row["物资名称"].ToString () + row["长度"] .ToString ()+ row["宽度"].ToString () + row["高度"].ToString () + "<br>";
                        }
                        newRow["GOODSNAME"] = row["物资名称"].ToString();
                        newRow["GOODSLONG"] = row["长度"].ToString();
                        newRow["GOODSWIDTH"] = row["宽度"].ToString();
                        newRow["GOODSHEIGHT"] = row["高度"].ToString();
                        newRow["REMARK"] = row["备注"].ToString();
                        tempDt.Rows.Add(newRow);
                   
                }

            this.FileUploadField2.Reset();
            if (!string.IsNullOrEmpty(message))
                Ext.Net.X.MessageBox.Alert("提示", "成功匹配:" + count + " 失败:" + (dt.Rows.Count - count) + "<br>无法匹配的物资:" + message).Show();
            Session["availablegoodsdt"] = tempDt;
            this.Store1.DataSource = (DataTable)Session["availablegoodsdt"];
            this.Store1.DataBind();
            
            }

调用的读取数据的方法

        public static DataTable ImportDataTableFromExcel(Stream excelFileStream, int sheetIndex, int headerRowIndex)
        {

            HSSFWorkbook workbook = new HSSFWorkbook(excelFileStream);
            HSSFSheet sheet = workbook.GetSheetAt(sheetIndex) as HSSFSheet;
            DataTable table = new DataTable();
            HSSFRow headerRow = sheet.GetRow(headerRowIndex) as HSSFRow;
            int cellCount = headerRow.LastCellNum;
            for (int i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                if (headerRow.GetCell(i) == null || headerRow.GetCell(i).StringCellValue.Trim() == "")
                {
                    cellCount = i + 1;
                    break;
                }
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                table.Columns.Add(column);
            }
            for (int i = (sheet.FirstRowNum + headerRowIndex + 1); i <= sheet.LastRowNum; i++)
            {
                HSSFRow row = sheet.GetRow(i) as HSSFRow;
                if (row == null || row.GetCell(0) == null || row.GetCell(0).ToString().Trim() == "")
                {
                    break;
                }
                DataRow dataRow = table.NewRow();
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    dataRow[j] = row.GetCell(j);
                }
                table.Rows.Add(dataRow);
            }
            excelFileStream.Close();
            workbook = null;
            sheet = null;
            return table;
        }
        public static DataTable ImportDataTableFromExcel(string excelFilePath, int sheetIndex, int headerRowIndex)
        {
            using (FileStream stream = System.IO.File.OpenRead(excelFilePath))
            {
                return ImportDataTableFromExcel(stream, sheetIndex, headerRowIndex);
            }
        }
--------------------编程问答-------------------- 能读到数据了  各位进来的 麻烦了 
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,