poi操作word模板,对原有的word修改

   /**
     * 化工园区调查表
     *
     * @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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值