POI生成word文档,再通过spire.doc.free 实现word转pdf

一、POI实现导出当前页面为word文档

1、导入poi依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

2、通过api创建模板,并导入数据

①.实体类

package com.example.pojo;
import lombok.Data;
@Data
public class User {
    private String name;
    private String sex;
    private String phone;
    private String idCard;
    private String post;
    private String level;
    private String status;
    private String dept;
}

②.构建并导出word

package com.example.wordtopdf;
import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class BuildWordDetail {

     private static void buildDoc(User user) throws IOException {

         XWPFDocument document = new XWPFDocument();// 创建Word文件
         XWPFParagraph paragraph = document.createParagraph();//创建段落
         XWPFRun run = paragraph.createRun();//创建段落文本
         run.setText(">>人员信息查询-详情");//文本内容
         run.setBold(true);//加粗
         run.setFontSize(14);//字体大小

         //创建段落
         XWPFParagraph paragraph1 = document.createParagraph();
         XWPFRun run1 = paragraph1.createRun();
         run1.setText("人员基本信息");
         run1.setBold(true);
         run1.setFontSize(12);

         //创建表格
         XWPFTable table = document.createTable(6, 4);
         //表格居中,还有其他
         table.setTableAlignment(TableRowAlign.CENTER);
        //遍历表格 设置每一列的 宽度  共4列
         for (XWPFTableRow row : table.getRows()) {
             row.getCell(0).setWidthType(TableWidthType.DXA);
             row.getCell(0).setWidth("1500");
             row.getCell(1).setWidthType(TableWidthType.DXA);
             row.getCell(1).setWidth("1500");
             row.getCell(2).setWidthType(TableWidthType.DXA);
             row.getCell(2).setWidth("2000");
             row.getCell(3).setWidthType(TableWidthType.DXA);
             row.getCell(3).setWidth("2000");
         }
         //第一行。第二列和第四列可以设置从库里查询的数据。b
         table.getRow(0).getCell(0).setText("面试人员:");
         table.getRow(0).getCell(1).setText(user.getName());
         table.getRow(0).getCell(2).setText("性别:");
         table.getRow(0).getCell(3).setText(user.getSex());
         //第二行
         table.getRow(1).getCell(0).setText("手机号:");
         table.getRow(1).getCell(1).setText(user.getPhone());
         table.getRow(1).getCell(2).setText("身份证:");
         table.getRow(1).getCell(3).setText(user.getIdCard());
      
         //创建段落
         XWPFParagraph paragraph2 = document.createParagraph();
         XWPFRun run2 = paragraph2.createRun();
         run2.setText("面试信息");
         run2.setBold(true);
         run2.setFontSize(12);

         //创建表格
         XWPFTable table1 = document.createTable(5, 4);
         table1.setTableAlignment(TableRowAlign.CENTER);

         for (XWPFTableRow row : table1.getRows()) {
             row.getCell(0).setWidthType(TableWidthType.DXA);
             row.getCell(0).setWidth("1500");
             row.getCell(1).setWidthType(TableWidthType.DXA);
             row.getCell(1).setWidth("1500");
             row.getCell(2).setWidthType(TableWidthType.DXA);
             row.getCell(2).setWidth("2000");
             row.getCell(3).setWidthType(TableWidthType.DXA);
             row.getCell(3).setWidth("2000");
         }

         table1.getRow(0).getCell(0).setText("面试岗位:");
         table1.getRow(0).getCell(1).setText(user.getPost());
         table1.getRow(0).getCell(2).setText("级别:");
         table1.getRow(0).getCell(3).setText(user.getLevel());

         XWPFParagraph paragraph3 = document.createParagraph();
         XWPFRun run3 = paragraph3.createRun();
         run3.setText("入职入场信息");
         run3.setBold(true);
         run3.setFontSize(12);

         XWPFTable table2 = document.createTable(8, 4);
         table2.setTableAlignment(TableRowAlign.CENTER);

         for (XWPFTableRow row : table2.getRows()) {
             row.getCell(0).setWidthType(TableWidthType.DXA);
             row.getCell(0).setWidth("1500");
             row.getCell(1).setWidthType(TableWidthType.DXA);
             row.getCell(1).setWidth("1500");
             row.getCell(2).setWidthType(TableWidthType.DXA);
             row.getCell(2).setWidth("2000");
             row.getCell(3).setWidthType(TableWidthType.DXA);
             row.getCell(3).setWidth("2000");
         }

         table2.getRow(0).getCell(0).setText("入职状态:");
         table2.getRow(0).getCell(1).setText(user.getStatus());
         table2.getRow(0).getCell(2).setText("入职部门:");
         table2.getRow(0).getCell(3).setText(user.getDept());

         //获取所有表格
         List<XWPFTable> tables = document.getTables();
         //遍历表格,删除表格所有边框
         tables.forEach(tableBorder -> {
             tableBorder.removeBorders();
         });
         //创建输出文件
         File file = new File("D:\\test.doc");
         //输出流
         FileOutputStream fileOutputStream = new FileOutputStream(file);
         //输出
         document.write(fileOutputStream);
         //关闭流
         fileOutputStream.close();
         System.out.println("word文件创建成功");
         }

    public static void main(String[] args) throws IOException {
        //设置数据
        User user = new User();
        user.setName("小红");
        user.setSex("女");
        user.setPhone("13122222222");
        user.setIdCard("412322222233333333");
        user.setPost("java");
        user.setLevel("初级");
        user.setStatus("未入职");
        user.setDept("银行组");
        //创建
        buildDoc(user);
    }

}

3.效果如下图

在这里插入图片描述
这里看到的表格线其实已经删除了,这是wps打开的,word就不会显示。表格主要是为了内容对齐,可根据需求设置。

二、通过e-iceblue的spire实现word转pdf

1、导入依赖

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>

依赖问题:

这个是免费版的jar包,不会有水印,不过好像有页数限制。但是华为云,阿里云并没有,需要手动下载手动导入或者配置setting。最后如果项目要打jar包部署服务器的话看下③。
①手动下载
a.地址:spire.doc.free.jar包下载地址
在这里插入图片描述
b.导入项目,第四步后选择下载的jar包,确定 ok。
在这里插入图片描述
c.找到pom.xml放入依赖就不会报错了

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>

②配置setting和pom
a.打开pom.xml放入下面的代码

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>
<repositories>
     <repository>
        <id>com.e-iceblue</id>
		<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
      </repository>
</repositories>

b.打开setting.xml文件,并设置如下
在这里插入图片描述

//<id>com.e-iceblue</id>
<mirrorOf>*,!com.e-iceblue</mirrorOf>

③以上两种本地都没问题,但是当我打jar包时还是无法把这个依赖一起打进去。安装jar到maven仓库
a.保存手动下载spire的jar包到某个目录下,最好不要放桌面
在这里插入图片描述
b.选中上面的目录路径,并输入cmd,回车。执行下面的命令即可安装到你的maven仓库。

mvn install:install-file -DgroupId=e-iceblue -DartifactId=spire.doc.free -Dversion=5.2.0 -Dfile=spire.doc.free-5.2.0.jar -Dpackaging=jar

在这里插入图片描述
c.此时的pom.xml依然要导入依赖,再

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>

其他:
打jar包命令

mvn clean install package -Dmaven.test.skip=true

在这里插入图片描述

2、word转pdf

上面依赖没问题的话就调api就好了。虽然这个免费但并不开源,进方法看代码是乱码的

package spiredoc;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class aaa {
    public static void main(String[] args) {
        //实例化Document类的对象
        Document doc = new Document();

        //加载Wordx(要转化的word文件)
        doc.loadFromFile("D:\\47_李星云.docx");

        //保存为PDF格式
        doc.saveToFile("D:\\WordToPDF.pdf", FileFormat.PDF);
    }
}

百度了很久,自己整理一下,下次看的明白。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐之谦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值