poi按word模板导出word、转换pdf导出
引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>5.2.2</version>
</dependency>
新建模板文件
在resource 下新建 word/xqpbd.docx
注意点
- word 模板建议全局使用宋体字体,以防止中文无法显示的问题
- linux导出pdf 需要将服务器增加宋体和WINGDNG2.TTF字体,否则会出现中文显示不了的问题
- 如遇缺少依赖包根据提示添加即可
代码示例
导出word
@RequestMapping("/doExport")
public void doExport(HttpServletResponse response) throws Exception {
try {
XWPFTemplate document = entityService.generateWordXWPFTemplate();
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=GDZB_PSD" + System.currentTimeMillis() + ".docx");
OutputStream os = response.getOutputStream();
document.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
导出pdf
@RequestMapping("/doExport")
public void doExport( HttpServletResponse response) throws Exception {
try {
XWPFTemplate document = entityService.generateWordXWPFTemplate();
byte[] array = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.writeAndClose(baos);
array = baos.toByteArray();
baos.close();
document.close();
InputStream inputStream = new ByteArrayInputStream(array);
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
inputStream.close();
xwpfDocument.close();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("批示单.pdf", "UTF-8"));
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
pdfBaos.writeTo(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(document, bos, out);
} catch (Exception e) {
e.printStackTrace();
}
}
service代码实现
@Override
public XWPFTemplate generateWordXWPFTemplate() throws ParseException, IOException {
// 业务代码填充字段
Map<String, Object> content = new HashMap<>();
String name = "word/xqpbd.docx";
content.put("name", "姓名");
TextRenderData textarea= new TextRenderData("R",new Style("Wingdings 2",10));
TextRenderData nullTextarea= new TextRenderData("\u25A1",new Style("宋体",10));
// 选中复选框
content.put("xzfxk",textarea);
// 空复选框
content.put("kfxk",nullTextarea);
InputStream inputStream =new ClassPathResource(name).getInputStream();
File file = File.createTempFile("temp","docx");
FileUtils.copyInputStreamToFile(inputStream,file);
return XWPFTemplate.compile(file).render(content);
}