• 解析Excel数据


    解析Excel数据常用的方式就是使用POI和JXL工具了,这两种工具使用起来有些类似,这里记录一下使用方式提供个参考

    POI使用

    File file = new File(filePath);
    FileInputStream fis = new FileInputStream(file);
    workbook = WorkbookFactory.create(fis);
    //poi3.6使用以下方式创建workbook
    //workbook = new HSSFWorkbook(fis);  //.xls格式
    int sheetNum = workbook.getNumberOfSheets();// Sheet的数量
    System.out.println("共[" + sheetNum + "]个工作表");
    for (int i = 0; i < sheetNum; i++) {
        System.out.println("开始读取第[" + (i + 1) + "]个工作表");
        Sheet sheet = workbook.getSheetAt(i);// 第i个Sheet
        String sheetName = sheet.getSheetName();
        System.out.println("工作表名称:" + sheetName);
        int rowNum = sheet.getPhysicalNumberOfRows();
        for (int j = 0; j < rowNum; j++) {
            System.out.println("开始读取第[" + (j + 1) + "]行数据:");
            Row row = sheet.getRow(j);
            int cellNum = row.getPhysicalNumberOfCells();
            for (int k = 0; k < cellNum; k++) {
                Cell cell = row.getCell(k);
                Object cellValue = null;
                   //根据单元格类型拿数据
                CellType cellType = cell.getCellTypeEnum();
                switch (cellType) {
                    case BOOLEAN:
                        cellValue = cell.getBooleanCellValue();
                        break;
                    case ERROR:
                        cellValue = cell.getErrorCellValue();
                        break;
                    case NUMERIC:
                        cellValue = cell.getNumericCellValue();
                        break;
                    case STRING:
                        cellValue = cell.getStringCellValue();
                        break;
                    default:
                        break;
                }
                
                //poi3.6使用以下方式判断单元格类型
                /*
                     switch (cell.getCellType()) {
                             case Cell.CELL_TYPE_BOOLEAN:
                                   cellValue = cell.getBooleanCellValue();
                                   break;
                             case Cell.CELL_TYPE_ERROR:
                                     cellValue = cell.getErrorCellValue();
                                     break;
                              case Cell.CELL_TYPE_NUMERIC:
                                     cellValue = cell.getNumericCellValue();
                                     break;
                              case Cell.CELL_TYPE_STRING:
                                     cellValue = cell.getStringCellValue();
                                     break;
                              default:
                                     break;
                                    }
                */
                
                System.out.printf("每个单元格的值:"+cellValue);
            }
        }
        
    }

    注意POI版本使用上有些许的不同,poi3.6及以上.xls和.xlsx格式的Excel对应的是不同的workbook 实现类, HSSFWorkbook只能操作excel2003以下版本(.xls),XSSFWorkbook只能操作excel2007以上版本(.xlsx),并且判断单元格类型使用的方法也有些许不同,需要注意下。

    JXL的使用

    Workbook book = Workbook.getWorkbook(new File(filePath));
    // 获得第一个表格对象
    Sheet sheet = book.getSheet(0);  //这里0代表只解析第一个sheet页
    // 拿到表格的行数
    int row = sheet.getRows();
    // 拿到表格的列数
    int col = sheet.getColumns();
    for (int j = 0; j < col; j++) {
        map = new HashMap<Integer, String>();
        for (int i = 0; i < row; i++) {
            Cell cell = sheet.getCell(j, i);
            String custContent = cell.getContents();
            //每个单元格的值
            System.out.printf("每个单元格的值:"+custContent);
        }
        
    /*      //判断单元格类型
            CellType cellType = cell.getType();
            if(cellType == CellType.EMPTY){
                 cell.getContents()                         //空值
            }else if(cellType == CellType.BOOLEAN){
                ((BooleanCell) cell).getValue();            //布尔值
            }else if(cellType == CellType.BOOLEAN_FORMULA){
                ((BooleanFormulaCell) cell).getValue();    //布尔值公式
            }else if(cellType == CellType.DATE){
                ((DateCell) cell).getDate();                //日期类
            }else if(cellType == CellType.DATE_FORMULA){
                ((DateFormulaCell)cell).getDate();            //日期公式
            }else if(cellType == CellType.NUMBER){
                ((NumberCell) cell).getValue();            //数字
            }else if(cellType == CellType.NUMBER_FORMULA){
                ((NumberFormulaCell) cell).getValue();        //数字公式
            }else if(cellType == CellType.STRING_FORMULA){
                ((StringFormulaCell) cell).getString();    //字符串公式
            }else if(cellType == CellType.ERROR){
                ((ErrorCell) cell).getContents();            //错误消息
            }else if(cellType == CellType.FORMULA_ERROR){
                ((ErrorFormulaCell) cell).getContents();    //公式错误
            }else element.setValue(cell.getContents());    
    */
  • 相关阅读:
    Vue 第六章 组件切换component、transition-group渲染
    Vue 第五章 钩子函数实现半场动画
    Vue 第四章 动画效果、animate第三方插件动画效果
    Vue 第三章 使用vue-resource列表案例、全局根域名配置
    hive介绍
    Hive性能优化--map数和reduce数
    Storm实现实时大数据分析
    hive架构原理简析-mapreduce部分
    hive入门学习线路指导
    HBase基本概念
  • 原文地址:https://www.cnblogs.com/lz2017/p/10085805.html
Copyright © 2020-2023  润新知