npoi读取excel

            string fileName = "***.xlsx";
            string AbsolutePath = "E:\\***\\***.xlsx";
            IWorkbook workbook = null;  //新建IWorkbook对象  
            FileStream fileStream = new FileStream(AbsolutePath, FileMode.Open, FileAccess.Read);
            if (fileName.IndexOf(".xlsx") > 0) // 2007版本  
            {
                workbook = new XSSFWorkbook(fileStream);  //xlsx数据读入workbook  
            }
            else if (fileName.IndexOf(".xls") > 0) // 2003版本  
            {
                workbook = new HSSFWorkbook(fileStream);  //xls数据读入workbook  
            }
            ISheet sheet = workbook.GetSheetAt(0);  //获取第一个工作表  
            IRow row;// = sheet.GetRow(0);            //新建当前工作表行数据 
            int i = 1;
            int num = 0;
            while (true)
            {
                row = sheet.GetRow(i);   //row读入第i行数据  
                if (row != null)
                {
                    //for (int j = 0; j < row.LastCellNum; j++)  //对工作表每一列  
                    row.GetCell(i).SetCellType(CellType.String);//设置i行j列数据为字符串类型
                    string iValue= row.GetCell(i).StringCellValue.ToString(); //获取i行j列数据 

                }
                else
                {
                    break;
                }
                i++;
            }

            fileStream.Close();
            workbook.Close();

 

posted @ 2018-06-28 13:57  过一天日子修一天缘  阅读(346)  评论(0编辑  收藏  举报