700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > POI生成word文档 再通过spire.doc.free 实现word转pdf

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

时间:2019-03-23 21:20:48

相关推荐

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;@Datapublic 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");}//第一行。第二列和第四列可以设置从库里查询的数据。btable.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-/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);}}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。