Excel读取返回List<Map>工具方法

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>
public static List<Map<String, Object>> readeExcelData(InputStream excelInputSteam,
                                                           int sheetNumber,
                                                           int headerNumber,
                                                           int rowStart,Integer rowEnd) throws IOException {
        System.out.println("请确保传入参数所有的下标都是行-1的index");
        List<Map<String, Object>> result = new ArrayList<>();
        List<String> headers = new ArrayList<>();
        Workbook workbook = WorkbookFactory.create(excelInputSteam);
//        XSSFWorkbook workbook = new XSSFWorkbook(excelInputSteam);
        Sheet sheet = workbook.getSheetAt(sheetNumber);
        Row header = sheet.getRow(headerNumber);
        //最后一行数据
        System.out.println("整个sheet(index="+sheetNumber+")最后一行index="+sheet.getLastRowNum());
        if(rowEnd==null){
            rowEnd=sheet.getLastRowNum();
            System.out.println("传入结束行号为空,则读取开始行后的所有行");
        }
        System.out.println("实际目标读取最后一行index="+rowEnd);
        DataFormatter dataFormatter = new DataFormatter();
        //获取标题信息
        for (int i = 0; i < header.getLastCellNum(); i++) {
            Cell cell = header.getCell(i);
            headers.add(dataFormatter.formatCellValue(cell));
        }
        System.out.println("head size="+headers.size());
        //获取内容信息
        for (int i = rowStart; i <= rowEnd; i++) {
            Row currentRow = sheet.getRow(i);
            if (Objects.isNull(currentRow)) {
                continue;
            }
            Map<String, Object> dataMap = new HashMap<>();
//            for (int j = 0; j < currentRow.getLastCellNum(); j++) {
            for (int j = 0; j < header.getLastCellNum(); j++) {
                //将null转化为Blank
                Cell data = currentRow.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                if (Objects.isNull(data)) {     //感觉这个if有点多余
                    dataMap.put(headers.get(j), null);
                } else {
                    dataMap.put(headers.get(j).replaceAll("\n", ""), getCellFormatValue(data));
                }
            }
            result.add(dataMap);
        }
        excelInputSteam.close();
        return result;
    }
    public static String getCellFormatValue(Cell cell){
        String cellValue = "";
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellTypeEnum()){
                case NUMERIC:{
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case ERROR:
                    cellValue="";
                    break; // 错误类型
                case _NONE:
                    cellValue="";
                    break;
                case BLANK:
                    cellValue="";
                    break;
                case FORMULA:{
                    try {
                        BigDecimal decimal=new BigDecimal(cell.getNumericCellValue());
                        cellValue = String.valueOf(decimal.setScale(4,BigDecimal.ROUND_HALF_UP));
                    } catch (IllegalStateException e) {
                        try {
                            cellValue = String.valueOf(cell.getRichStringCellValue());
                        }catch (IllegalStateException e1) {
                            cellValue="";
                        }
                    }
                    break;
                }
                case STRING:{
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue.trim();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可——叹——落叶飘零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值