poi按word模板导出word、转换pdf导出

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

注意点

  1. word 模板建议全局使用宋体字体,以防止中文无法显示的问题
  2. linux导出pdf 需要将服务器增加宋体和WINGDNG2.TTF字体,否则会出现中文显示不了的问题
  3. 如遇缺少依赖包根据提示添加即可

代码示例

导出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);
}
在使用easy-poi导出word模板中的表格时,需要按照以下步骤进行操作。 首先,我们需要准备一个word模板文件,其中包含了我们想要导出的表格的样式和布局。可以使用Microsoft Word或其他支持word格式的编辑软件创建和编辑模板。 接下来,我们需要使用easy-poi的API来读取和处理模板文件。首先,我们需要创建一个`TemplateExportParams`对象,指定模板文件的路径。 然后,我们可以通过调用`ExcelExportUtil.exportWord`方法来根据模板生成word文件。在导出过程中,我们可以使用`Map`或`List<Map>`对象作为数据源,用于填充模板中的表格单元格。 对于简单的表格,我们可以使用`Map`对象来存储数据。其中,键对应模板中的字段名,值对应字段要显示的数据。如果我们需要填充多行表格,可以使用`List<Map>`来存储多个`Map`对象。 在代码中,我们可以使用以下语句来导出word文件: ```java String templatePath = "模板文件路径"; String outputPath = "导出文件保存路径"; TemplateExportParams exportParams = new TemplateExportParams(templatePath); Map<String, Object> map = new HashMap<>(); map.put("表格数据", 数据源); Workbook workbook = ExcelExportUtil.exportWord(exportParams, map); FileOutputStream fos = new FileOutputStream(outputPath); workbook.write(fos); fos.close(); ``` 其中,"表格数据"是模板中指定的字段名,数据源是存储表格数据的`Map`或`List<Map>`对象。 最后,我们可以保存生成的word文件到指定的输出路径。通过调用`workbook.write`方法将`Workbook`对象写入到输出流中,即可保存为word文件。 以上就是使用easy-poi导出word模板表格的主要步骤。通过简单配置模板和填充数据,我们可以轻松地生成符合需要的word文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值