DOCX 文档常用操作 - 转富文本、装PDF、提取变量、变量赋值

3 篇文章 0 订阅
2 篇文章 0 订阅

基础包

基于poi 4.1.0

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

转富文本

增加依赖
<dependency>
	<groupId>fr.opensagres.xdocreport</groupId>
	<artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
	<version>2.0.2</version>
</dependency>
测试代码
@Test
public void docxToHtml() throws IOException {
    String filePath = "D:\\test1.docx";
    String fileHtmlPath = "D:\\test3.html";
    // 将上传的文件传入Document转换
    XWPFDocument document = new XWPFDocument(Files.newInputStream(Paths.get(filePath)));
    XHTMLOptions options = XHTMLOptions.create();
    BufferedWriter output = new BufferedWriter(new FileWriter(fileHtmlPath));
    XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
    xhtmlConverter.convert(document, output, options);
    output.close();
}

转PDF

增加依赖
<dependency>
	<groupId>fr.opensagres.xdocreport</groupId>
	<artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
	<version>2.0.2</version>
</dependency>
测试代码
@Test
public void docxToPdf() throws IOException {
    String filePath = "D:\\test1.doc";
    String fileHtmlPath = "D:\\test4.pdf";
    FileInputStream fileInputStream = new FileInputStream(filePath);
    XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
    PdfOptions pdfOptions = PdfOptions.create();
    FileOutputStream fileOutputStream = new FileOutputStream(fileHtmlPath);
    PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
    fileInputStream.close();
    fileOutputStream.close();
}

提取变量

增加依赖
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.7.3</version>
</dependency>
测试代码
@Test
public void getTl() {
    String filePath = "D:\\test1.docx";
    ConfigureBuilder builder = Configure.newBuilder();
    // 标签前后缀外的任意字符
    builder.buildGrammerRegex(RegexUtils.createGeneral("{{", "}}"));
    XWPFTemplate template = XWPFTemplate.compile(filePath, builder.build());
    // 文档中所有 {{变量}} 集合
    List<String> collect = template.getElementTemplates().stream().map(Object::toString).collect(Collectors.toList());
}

变量赋值

增加依赖
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.7.3</version>
</dependency>
测试代码
@Test
public void variableAssignment() throws IOException {
    String filePath = "D:\\test2.docx";
    String targetPath = "D:\\test2Download.docx";
    XWPFTemplate template = XWPFTemplate.compile(filePath);
    Map<String, Object> parameterMap = new HashMap<>();
    // 测试文本
    parameterMap.put("lessor_bGVzc29y", new TextRenderData("ff4757", "Sayi"));
    // 测试图片 模板变量需要@开始
    parameterMap.put("image", new PictureRenderData(50, 50, "D:\\Windows\\Pictures\\测试图片.jpeg"));
    // 测试表格 模板变量需要#开始
    RowRenderData header = RowRenderData.build(new TextRenderData("ff4757", "日期"), new TextRenderData("ff4757", "金额"));
    RowRenderData row0 = RowRenderData.build("2022-10-17", "100");
    RowRenderData row1 = RowRenderData.build("2022-9-17", "100");
    RowRenderData row2 = RowRenderData.build("2022-8-17", "100");
	parameterMap.put("table", new MiniTableRenderData(header, Arrays.asList(row0, row1, row2), 10));
    template.render(parameterMap);
    template.writeToFile(targetPath);
}

DOC 转 DOCX

增加依赖
<!-- doc to docx 可能存在版权问题 -->
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>3.9.0</version>
</dependency>

测试代码

支持多种格式相互转换,使用是自行测试

@Test
public void docToDocx() throws IOException {
    String filePath = "D:\\test1.doc";
    String fileHtmlPath = "D:\\test2.docx";
    com.spire.doc.Document document = new com.spire.doc.Document ();
    document.loadFromFile(filePath);
    document.saveToFile(fileHtmlPath, FileFormat.Docx);
}
常见问题
  1. 部署到 linux 存在缺失字符库问题,需要导入字符库
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值