需求:生成一份合同,合同内容清单部分替换,其余文字统一不变。
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。