通过EXCEL文件模板获取并解析文件内容

public List<Map<String, Object>> mxsxExcel(String fileName, MultipartFile file) throws Exception {

//fileName 文件名称

//file文件
//        存放所有行数据
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        // 保存标题
        List<String> columnTitleList = new ArrayList<String>();
        boolean notNull = false;

//校验文件是否是xls文件
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;

//校验文件是否是xlsx文件
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

//获取文件流数据
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if (sheet != null) {
            notNull = true;
        }

        //获取列标题
        if (columnTitleList.size() == 0) {
            /* 1、读取表头操作 */
            Row row = sheet.getRow(2); // 读取第二行
            short totalCells = row.getLastCellNum();
            for (short c = 0; c < totalCells; c++) {// 读取列,从第一列开始
                Cell cell = row.getCell(c);
                if (cell == null) {
                    // columnTitleList.add(ExcelUtil.EMPTY);
                    continue;
                }
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String title = cell.getStringCellValue().trim();
                if (!ExcelUtil.EMPTY.equals(title)) {
                    columnTitleList.add(title);
                }
            }
        }

        int totalRows = sheet.getLastRowNum();
        //从第四行开始读取数据
        for (int rownum = 3; rownum <= totalRows; rownum++) {
            //        存放每一行数据
            Map<String, Object> map = new HashMap<>();
            Row row = sheet.getRow(rownum);
            if (row == null) {
                continue;
            }
            short totalCells = (short) columnTitleList.size();
            String title = null;
            for (short c = 0; c < totalCells; c++) {
                Cell cell = row.getCell(c);
                title = columnTitleList.get(c);
                if (cell == null) {
                    if("属性名称".equals(title)||"属性长度".equals(title)||"属性类型".equals(title)){
                        System.out.println("第"+rownum+1+"行有未输入的必填项,添加失败");
                    }
                    map.put(title, ExcelUtil.EMPTY);
                    continue;
                }
//              设置成字符串
                cell.setCellType(Cell.CELL_TYPE_STRING);
                map.put(title, cell.getStringCellValue().trim());
            }
            dataList.add(map);
        }
        return dataList;

    }

属性模板表(红色列为必填项)
请输入属性名请输入数字请输入数字 
属性名称属性长度属性类型属性描述

                                                                                                     模板示例图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值