基础包
基于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);
}
常见问题
- 部署到 linux 存在缺失字符库问题,需要导入字符库