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

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

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

  1. java后台处理excel_java后台利用Apache poi 生成excel文档提供前台下载示例

    之前在项目中会用到在java在后台把数据填入Word文档的模板来提供前台下载,为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客,访问量已经是我写的博客里第一了.于是乎我在学会用Java在后 ...

  2. java根据html生成word文档,Java之HTML富文本导出WORD(不含图片)

    一.需求: 我们在使用富文本编辑器来编辑文本的时候,文本会自带HTML的标签比如 等来修饰字体样式. 比如ueditor.kindeditor等富文本编辑器. 那么,我们如何将富文本编辑器里的内容导出 ...

  3. web系统中巧用word文档的html格式创建多样式的word文档,WEB系统中巧用WORD文档的HTML格式创建多样式的WORD文档...

    以计算机和现代网络技术为特征的现代信息技术极大地促进了社会经济的发展,基于各行各业的WEB系统的开发与应用也越来越多. >> WEB系统中巧用WORD文档的HTML格式创建多样式的WORD ...

  4. word文档docx密码忘了怎么办,word文档docx权限限制怎么办?

    word文档docx移除编辑密码,word文档docx权限限制如何解除?[解密神器]word文档docx权限密码解密,用「密码帝」啊 超级好用!,百度搜索密码帝就可以了,非常简单手机电脑都可以用,一键 ...

  5. c#取消word修订痕迹_C# 设置、删除、读取Word文档背景——基于Spire.Cloud.Word

    Spire.Cloud.Word.Sdk提供了接口SetBackgroudColor().SetBackgroudImage().DeleteBackground().GetBackgroudColo ...

  6. 国产文档控件Spire.Doc for.NET ,增强从 Word 到 PDF 和 HTML 的转换

    Spire.Doc pack (hotfix) 11.5.12 增强了从 Word 到 PDF 和 HTML 的转换 很高兴地宣布发布 Spire.Doc 11.5.12.此版本增强了从 Word 到 ...

  7. Freemarker下载Word文档(文字+图片+表格)Idea示例 (附word展示图片异常解决方案)(附JPEG格式图片通过imageio.read方法读取为null解决方案)

    流程:拿到word模板 转为ftl格式并填充占位符参数,调用java代码填充参数即可 (文末附word打开图片显示异常,wps打开却显示正常的解决方案) (文末附Jpeg格式图片获取为null解决方案 ...

  8. C#操作word文档,复制指定页面至新的word

    word类库目前用得较多的有a. Free Spire.Doc for .NET  b. NPOI  c. Microsoft.Office.Interop.Word 其中Spire免费版的有页数限制 ...

  9. 微信公众号添加word文档附件教程_公众号添加Excel、PDF、PPT等附件教程

    写公众号文章时,有时候希望在公众号文末分享一些文档如Word.Excel.PDF之类的文档,但苦于公众号并不支持直接上传文件.插入超链接是个方案,但并不是所有的公众号都有此功能.并且微信内并不允许直接 ...

最新文章

  1. node.js搭建简单服务器,用于前端测试websocket链接方法和性能测试
  2. python项目实战:最简单的图片转字符画
  3. mysql怎么删除唯一索引_mysql删除唯一索引
  4. Python 进阶 —— defaultdict
  5. 2012服务器在IIS部署的SLL(https)网址谷歌浏览器无法访问的问题解决
  6. python系统学习_【Python系统学习】基础篇
  7. 电力-104规约实际测试1
  8. python博弈论代码_博弈论的算法总结
  9. AMD Intel 机器 Spark 性能测试
  10. 接口测试简介以及接口测试用例设计思路
  11. Second copy 的增量备份
  12. 从美国人工智能年会看2017世界人工智能最新研究成果
  13. 《摄影测量学》空间后方交会详细解读
  14. android百度地图3d路线,百度地图Android V2.0新增卫星图及3D模式功能
  15. 香港服务器怎么加速?
  16. python编程技术解决英语单词测试(包括添加英文单词、查询英文单词和查询英文单词)以及绘制雷达图功能
  17. 我用最独特的方式为情人节准备了这些。。。
  18. 13.14.4 文本文件到电子表格。python编程快速上手--让繁琐工作自动化 第2版。【美】阿尔·斯维加特 Al Sweigart 著 王海鹏 译 中国工信出版集团 人民邮电出版社
  19. CenterNet环境搭建记录
  20. Vector space

热门文章

  1. 拥有多重人格的他,可能是个裂脑人
  2. 2022年湖南省高职单招(语文)考试冲刺试题及答案
  3. java websocket即时通讯+layui实现移动端一对一聊天客服功能
  4. 【Linux篇】kali Linux系统一次性安装软件包
  5. 前端学习JS第八天(P102--P110)
  6. e站host地址_台达变频器VFD-E 系列MODBUS-RTU详解
  7. 电信宽带免费提速到200M!不用安装小翼管家!
  8. 【计算机视觉】图片拼接
  9. 广东省计算机考试报名照片规格,计算机等级考试报名 有关照片规格及要求介绍...
  10. MC服安装Geyser使基岩版玩家进入JE服务器