欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

使用poi组件读取excle文件,poi组件读取excle,public void

来源: javaer 分享于  点击 21106 次 点评:220

使用poi组件读取excle文件,poi组件读取excle,public void


public void importPostGroupQuality() throws Exception {        //InputStream fis=new FileInputStream("E:\\员工年度考核.xls");        InputStream fis=this.path();        //解析出来的Excel的数据存放的List集合        //解析出来的Excel中的每一条数据封装的实体        try {            //创建Excel工作薄             HSSFWorkbook hwb = new HSSFWorkbook(fis);                     //得到第一个工作表             HSSFSheet sheet = hwb.getSheetAt(0);             HSSFRow row = null;  //            System.out.println("一共:"+hwb.getNu);            //遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数              for(int i = 0; i < hwb.getNumberOfSheets(); i++) {                              sheet = hwb.getSheetAt(i);                System.out.println("一共多少行:"+sheet.getPhysicalNumberOfRows());                //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数                 for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {                     row = sheet.getRow(j);                    System.out.println("一共有列:"+row.getPhysicalNumberOfCells());                    int total=row.getPhysicalNumberOfCells();                    for(int a=0;a<total;a++){                        if(getCellValue(row.getCell(a))!=""){                            System.out.print(getCellValue(row.getCell(a))+":");                        }                    }                }            }            } catch (Exception e) {             fis.close();            e.printStackTrace();         }     }  //判断从Excel文件中解析出来数据的格式      public  String getCellValue(HSSFCell cell){          String value = null;          //简单的查检列类型          switch(cell.getCellType())           {              case HSSFCell.CELL_TYPE_STRING://字符串                  value = cell.getRichStringCellValue().getString();                  break;              case HSSFCell.CELL_TYPE_NUMERIC://数字                  long dd = (long)cell.getNumericCellValue();                  value = dd+"";                  break;              case HSSFCell.CELL_TYPE_BLANK:                  value = "";                  break;                 case HSSFCell.CELL_TYPE_FORMULA:                  value = String.valueOf(cell.getCellFormula());                  break;              case HSSFCell.CELL_TYPE_BOOLEAN://boolean型值                  value = String.valueOf(cell.getBooleanCellValue());                  break;            case HSSFCell.CELL_TYPE_ERROR:                  value = String.valueOf(cell.getErrorCellValue());                  break;              default:                  break;          }          return value;      } 

其中InputStreamfis=this.path(); 获取文件流,请根据实际情况进行修改,获取本地文件的输入流代码是:InputStreamfis=newFileInputStream(“E:\员工年度考核.xls”);

相关栏目:

用户点评