/*** 化工园区调查表** @param templatePath* @param outPath* @param parkInterview*/public static String getDocx(String templatePath, String outPath, ParkInterview parkInterview){File file = new File(templatePath);File file1 = new File(outPath);if(!file1.exists()) file1.mkdirs();String outPath1=file1.getPath()+File.separator+parkInterview.getSpotCountNumber()+"_"+"化工园区信息调查表.docx";String outPath2=file1.getPath()+File.separator+parkInterview.getSpotCountNumber()+"_"+"化工园区信息调查表.pdf";InputStream in = null;try {in = new FileInputStream(file);XWPFDocument document = new XWPFDocument(in);List<XWPFTable> tables = document.getTables();List<XWPFTableRow> rows = tables.get(0).getRows();int line=1;XWPFTableCell cell = rows.get(line).getTableCells().get(1);cell.setText(parkInterview.getName());//化工园区名称cell = rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getDate());//访谈日期cell = rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getInterviewerName());//访谈人员姓名cell = rows.get(line).getTableCells().get(3);cell.setText(parkInterview.getInterviewerMobile());//访谈人员电话cell=rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getInterviewerOrganization());//访谈人员单位cell=rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getIntervieweeName());//受访人姓名cell=rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getIsEvaluation());//是否开展过场地环境调查评估工作cell=rows.get(++line).getTableCells().get(1);cell.setText(parkInterview.getOtherProblem());//其他土壤或地下水污染相关疑问ByteArrayOutputStream bos = new ByteArrayOutputStream();OutputStream out = Files.newOutputStream(Paths.get(outPath1));document.write(bos);out.write(bos.toByteArray());out.close();bos.close();in.close();File file2 = new File(outPath1);ZipSecureFile.setMinInflateRatio(0.0000001);FileInputStream inputStream = new FileInputStream(file2);XWPFDocument document2 = new XWPFDocument(inputStream);for (XWPFTableRow row : document2.getTables().get(0).getRows()) {for (XWPFTableCell tableCell : row.getTableCells()) {for (XWPFParagraph paragraph : tableCell.getParagraphs()) {for (XWPFRun run : paragraph.getRuns()) {run.setFontSize(11);}}}}ByteArrayOutputStream bos1 = new ByteArrayOutputStream();OutputStream out1 = new FileOutputStream(outPath1);document2.write(bos1);out1.write(bos1.toByteArray());out1.close();} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}return outPath;}
效果图:
原表
poi操作之后:
注意
- 调整字体大小需要先生成一份word,再重新读word进行修改
- poi也可以不使用模板,一行一行读
- word转pdf不好转,尝试了格式变动了
- 引入的依赖
implementation("org.apache.poi:poi:4.0.1")implementation("org.apache.poi:poi-ooxml:4.0.1")