继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

poi excel读取 兼容xlsm,xlsx

冰菓的呐喊
关注TA
已关注
手记 2
粉丝 3
获赞 23

需要jar包:poi-3.7-20101029.jar
poi-examples-3.7-20101029.jar
poi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar
poi-scratchpad-3.7-20101029.jar
xmlbeans-2.3.0.jar

/**

  • @param filepath //文件路径
  • @param filename //文件名
  • @param startrow //开始行号
  • @param startcol //开始列号
  • @param sheetnum //sheet
  • @return list
    */
    public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol, int sheetnum) {
    List<Object> varList = new ArrayList<Object>();

    try {
        File target = new File(filepath, filename);
        FileInputStream fi = new FileInputStream(target);
        Workbook wb = null; 
        try{
             wb = new XSSFWorkbook(fi);
    
        }
         catch (Exception ex) { 
             wb = new HSSFWorkbook(new FileInputStream(target));
            } 
        Sheet sheet = wb.getSheetAt(sheetnum);                  //sheet 从0开始
    
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); 
        int rowNum = sheet.getLastRowNum() + 1;                     //取得最后一行的行号
    
        for (int i = startrow; i < rowNum; i++) {                   //行循环开始
    
            PageData varpd = new PageData();
            Row row = sheet.getRow(i);                          //行
            int cellNum = row.getLastCellNum();                     //每行的最后一个单元格位置
    
            for (int j = startcol; j < cellNum; j++) {              //列循环开始
    
                DecimalFormat df = new DecimalFormat("0.000000");  
                Cell cell1 = row.getCell(Short.parseShort(j + ""));
                CellValue cell = evaluator.evaluate(cell1);
                String cellValue = null;
                if (null != cell) {
                    switch (cell.getCellType()) {                   // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
                    case 0:
                        cellValue = df.format(cell.getNumberValue());
                        break;
                    case 1:
                        cellValue = cell.getStringValue();
                        break;
                    case 2:
                        cellValue = cell.getNumberValue() + "";
                        // cellValue = String.valueOf(cell.getDateCellValue());
                        break;
                    case 3:
                        cellValue = "";
                        break;
                    case 4:
                        cellValue = String.valueOf(cell.getBooleanValue());
                        break;
                    case 5:
                        cellValue = String.valueOf(cell.getErrorValue());
                        break;
                    }
                } else {
                    cellValue = "";
                }
    
                varpd.put("var"+j, cellValue);
    
            }
            varList.add(varpd);
        }
    
    } catch (Exception e) {
        System.out.println(e);
    }
    
    return varList;

    }

打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP

热门评论

这个真的兼容xlsm吗

查看全部评论