poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)

选中图片右击  选择设置图片格式

 

例如word模板

 

maven依赖

        <!-- java 读取word文件里面的加颜色的字体  转pdf 使用  -->
        <dependency>
            <groupId> e-iceblue </groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
 
  <!--poi 的相关组件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
 
        <!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
 
        <!-- 用于  word 转pdf  -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.2</version>
        </dependency>
 
        <!-- pdf 添加水印  对PDF文件的操作 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.1</version>
        </dependency>
        <!-- PDF文件 字体 防止中文乱码  -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
 
        <!--基于 poi实现word数据的替换 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>
 
 
  
 
 
        <!--
         如果下载jar失败,说明下载jar失败,需要以下的maven依赖
         [ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
  --> 
        <repository>
            <id>com.e-iceblue</id>
               <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
           </repository>
      </repositories>
 
 
        <!--        <dependency>
                    <groupId>org.apache.pdfbox</groupId>
                    <artifactId>pdfbox</artifactId>
                    <version>2.0.25</version>
                </dependency>
               <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
                    <version>1.0.6</version>
                </dependency>
               <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>fr.opensagres.xdocreport.document</artifactId>
                    <version>2.0.2</version>
                </dependency>
                <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
                    <version>1.0.6</version>
                </dependency>-->
 
 
 
 
 
 
    <!-- 打包使用,需要配置 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 打包时会将本地jar一起打包 -->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
 

 

  <!--基于 poi实现word数据的替换 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>

<!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>

<!-- 用于  word 转pdf  -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.2</version>
        </dependency>

读取

 代码

 @GetMapping("exportPDF")
    public AjaxResult exportPDF(HttpServletResponse response) throws Exception {
        LoginUser loginUser = getLoginUser();
        OpenCompanyInfo companyInfo = openCompanyInfoService.selectEnterpriseInfo(loginUser.getId());
        if (companyInfo != null) {
            Map<String, Object> data = new HashMap<>();
            data.put("legalDeputy", companyInfo.getLegalDeputy());
            data.put("companyName", companyInfo.getCompanyName());
            data.put("companyCode", companyInfo.getCompanyCode());
            if (StringUtils.isNoneBlank(companyInfo.getBusinessLicenseUrl())) {
                InputStream businessLicenseUrl = minioClientUtils.returnInputStream(null, companyInfo.getBusinessLicenseUrl());
                data.put("businessLicenseUrl", Pictures.ofStream(businessLicenseUrl, PictureType.JPEG).size(120, 120).create());
            }
            if (StringUtils.isNoneBlank(companyInfo.getCompanyLogoUr())) {
                InputStream companyLogoUrl = minioClientUtils.returnInputStream(null, companyInfo.getCompanyLogoUr());
                data.put("companyLogoUrl", Pictures.ofStream(companyLogoUrl, PictureType.JPEG).size(120, 120).create());
            }
            InputStream inputStreamFile = ResourceReader.class.getResourceAsStream("/templates/模版.docx");
            XWPFTemplate template = XWPFTemplate.compile(inputStreamFile).render(data);
//            XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\11949\\Desktop\\模版.docx").render(data);
            byte[] array = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            template.writeAndClose(baos);
            array = baos.toByteArray();
            baos.close();
            template.close();
            InputStream inputStream = new ByteArrayInputStream(array);
            ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
            fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
            inputStream.close();
            xwpfDocument.close();
            response.setContentType("application/octet-stream");
            String format = DateUtil.format(new Date(), "yyyy-MM-dd");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("认证授权函" + format + ".pdf", "UTF-8"));
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            pdfBaos.writeTo(bos);
            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        }
        return null;
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!您可以使用poi-tl库来导出带有合并列的Word表格,并将其下载。下面是一个简单的示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign; import java.io.FileOutputStream; import java.io.IOException; public class WordTableMergeColumnsExample { public static void main(String[] args) { // 创建一个新的文档 XWPFDocument document = new XWPFDocument(); // 创建一个表格 XWPFTable table = document.createTable(3, 3); // 合并第一行的前两列 table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); table.getRow(0).getCell(0).setText("Merged Cells"); table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); // 设置表格中其他单元格的内容 table.getRow(1).getCell(0).setText("Cell 1"); table.getRow(1).getCell(1).setText("Cell 2"); table.getRow(1).getCell(2).setText("Cell 3"); table.getRow(2).getCell(0).setText("Cell 4"); table.getRow(2).getCell(1).setText("Cell 5"); table.getRow(2).getCell(2).setText("Cell 6"); // 设置表格样式 CTTblPr tblPr = table.getCTTbl().getTblPr(); CTTblWidth tblWidth = tblPr.getTblW(); tblWidth.setW(BigInteger.valueOf(5000)); // 调整表格中所有单元格的垂直对齐方式 for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { cell.setVerticalAlignment(XWPFVertAlign.CENTER); } } // 将文档保存到文件中 try (FileOutputStream out = new FileOutputStream("merged_table.docx")) { document.write(out); System.out.println("表格导出成功!"); } catch (IOException e) { System.out.println("表格导出失败:" + e.getMessage()); } } } ``` 这个示例代码创建了一个3x3的表格,并将第一行的前两列进行了合并。您可以根据自己的需求修改表格内容和样式。最后,将文档保存到名为 "merged_table.docx" 的文件中。 希望对您有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值