使用poi操作excel模板记录

需求:生成一份合同,合同内容清单部分替换,其余文字统一不变。

maven依赖:

            <!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi-ooxml-schemas.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>

版本:

excel模板:

说明:excel模板替换后的格式可直接在模板上设置,比较方便。模板上有数据是为了设置格式,代码可直接替换内容。也可使用word模板:使用poi-tl操作word模板记录

完整代码,仅供参考

    public SysFile orderContractGenExcel(MachineOrderResult detail) {
        try {
            // 从resources下获取模板
            ClassPathResource classPathResource = new ClassPathResource("template/htmb.xlsx");
            InputStream is = classPathResource.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            XSSFSheet sheet = workbook.getSheetAt(0);
            // 获取模板行格式
            Row tempRow = sheet.getRow(7);
            CellStyle tempCellStyle = tempRow.getCell(1).getCellStyle();
            // 金额小写格式
            Row totalPriceLowerCaseRow = sheet.getRow(8);
            CellStyle totalPriceLowerCaseStyle = totalPriceLowerCaseRow.getCell(9).getCellStyle();
            // 金额大写格式
            Row totalPriceUpperCaseRow = sheet.getRow(9);
            CellStyle totalPriceUpperCaseStyle = totalPriceUpperCaseRow.getCell(9).getCellStyle();
            double totalPrice = 0;
            // 向下移动n行,避免覆盖到
            sheet.shiftRows(8, sheet.getLastRowNum(), detail.getParts().size() - 1);
            // 设置表格数据
            for (int i = 0; i < detail.getParts().size(); i++) {
                totalPrice += detail.getParts().get(i).getTotalPrice();
                Row row = sheet.createRow(i + 7);
                row.createCell(0).setCellValue(i + 1);  //序号
                row.createCell(1).setCellValue(detail.getParts().get(i).getProjectCode().trim());
                row.createCell(2).setCellValue(detail.getParts().get(i).getHierarchy().trim());
                row.createCell(3).setCellValue(detail.getParts().get(i).getPartCode().trim() + "/" + detail.getParts().get(i).getPartVersion().trim());
                row.createCell(4).setCellValue(detail.getParts().get(i).getPartName().trim());
                row.createCell(5).setCellValue(detail.getParts().get(i).getProcessNumber());
                row.createCell(6).setCellValue(detail.getParts().get(i).getRawMaterial().trim());
                row.createCell(7).setCellValue("PCS");
                row.createCell(8).setCellValue(detail.getParts().get(i).getUnitPrice());
                row.createCell(9).setCellValue(detail.getParts().get(i).getTotalPrice());
                row.createCell(10).setCellValue(detail.getParts().get(i).getReceiveTime());
                row.createCell(11).setCellValue(detail.getParts().get(i).getRemark());
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null) {
                        // 将CellStyle应用到单元格上
                        cell.setCellStyle(tempCellStyle);
                    }
                }
            }
            XSSFRow totalLowerRow = sheet.getRow(7 + detail.getParts().size());
            XSSFCell totalLowerCell = totalLowerRow.getCell(9);
            totalLowerCell.setCellValue(totalPrice);
            totalLowerCell.setCellStyle(totalPriceLowerCaseStyle);
            XSSFRow totalUpperRow = sheet.getRow(8 + detail.getParts().size());
            XSSFCell totalUpperCell = totalUpperRow.getCell(9);
            totalUpperCell.setCellValue(totalPrice);
            totalUpperCell.setCellStyle(totalPriceUpperCaseStyle);

            // 将word转为输出流
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            workbook.write(outStream);
            workbook.close();
            // 3将输出流转为 multipartFile 并上传
            MockMultipartFile multipartFile = new MockMultipartFile("file", detail.getSerialNo() + "合同" + System.currentTimeMillis() + ".xlsx", null, outStream.toByteArray());
            outStream.close();
            R<SysFile> upload = fileService.upload(multipartFile, 1);
            return upload.getData();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

遇到问题:

将模板放入resource目录下,无法读取,maven打包时,报错MalformedInputException。

解决方法:解决 `MalformedInputException: Input length = 1` 错误

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值