当前位置: 首页 > news >正文

湖南房产网关键词优化公司电话

湖南房产网,关键词优化公司电话,新乡app开发公司,网站建设分金手指排名十三使用 poi-tl 根据模板生成 word 文件。 使用 xdocreport 将 docx 文件转换为 pdf 文件。 xdocreport 也支持根据模板导出 word ,但是 poi-tl 的功能更齐全,操作更简单,文档清晰。 poi-tl 、xdocreport 内部均依赖了 poi ,要注意两…

使用 poi-tl 根据模板生成 word 文件。
使用 xdocreport 将 docx 文件转换为 pdf 文件。

xdocreport 也支持根据模板导出 word ,但是 poi-tl 的功能更齐全,操作更简单,文档清晰。
poi-tl 、xdocreport 内部均依赖了 poi ,要注意两者中 poi 和 自身项目引用的 poi 的版本是否存在冲突。

文章目录

    • Pom 依赖
    • 生成 DOCX 文件
      • 创建 DOCX 模板
      • 生成 DOCX 文档
        • 创建模板填充类
        • 生成文件(多种方式)
        • 生成效果
      • DOCX 转 PDF
        • PDF 生成效果
        • 注意

Pom 依赖

使用 poi 5.2.2 ,poi-tl 1.12.1 ,xdocreport 2.0.3

<!--        poi依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version></dependency>
<!--        poi-tl依赖--><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version></dependency>
<!--        xdocreport依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-full</artifactId><version>5.2.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>xdocreport</artifactId><version>2.0.3</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency>

生成 DOCX 文件

创建 DOCX 模板

根据官方文档按要求创建模板,并放在resources文件夹下。官方文档 http://deepoove.com/poi-tl/。
在这里插入图片描述
在这里插入图片描述

生成 DOCX 文档

创建模板填充类

其实也可以在生成文件时使用 Map 类型的方式填充文件内容,如下

XWPFTemplate.compile(inputStream).render(new HashMap<String, Object>(){{put("title", "Hi, poi-tl Word模板引擎");
}});

但是使用实体类更规范一些。

@Data
public class ChildRoundsProvalDocxEntity {private String childName;//孩子姓名private String identify;//身份证号private String gender;//性别private String childRounds;//孩次private PictureRenderData avatar;//头像地址private String entryDate;//入院时间private String gardenMonths;//孩次private String charge;//每月收费private String fatherName;//父亲姓名private String fatherIdentify;//父亲身份证号private String motherName;//母亲姓名private String motherIdentify;//母亲身份证号private String address;//住址及联系方式private String firstChildName;//第一个子女姓名private String firstChildIdentify;//第一个子女身份证号private String secondChildName;//第二个子女姓名private String secondChildIdentify;//第二个子女身份证号private String signDate;//签署日期
}

生成文件(多种方式)

1、生成一个文件输入流,方便用于再次编辑
不要在方法内部将输入流关闭

/*** 生成文件到输入流中* @param templatePath* @param data* @return*/
public InputStream createWordFile1(Object data){Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;InputStream resultStream = null;try {// word模板填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);resultStream = PoitlIOUtils.templateToInputStream(template);PoitlIOUtils.closeQuietlyMulti(template);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}return resultStream;
}

2、生成docx到输出流中,一般是在网络响应中直接输出,浏览器去下载

public void createWordFile2(Object data, HttpServletResponse httpServletResponse){//获取模板信息Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;docName = URLEncoder.encode(docName, StandardCharsets.UTF_8);try {httpServletResponse.setContentType("application/octet-stream");httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + docName + ".docx");httpServletResponse.addHeader("filename", docName);// word模板内容填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);OutputStream out = httpServletResponse.getOutputStream();//要记得关闭BufferedOutputStream bos = new BufferedOutputStream(out);//要记得关闭template.write(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template, bos, out);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());throw new BaseException("Word文档生成失败");} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}
}

3、直接生成文件到指定路径

public void createWordFile3(Object data, String path){//获取模板信息Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;try {// word模板内容填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);template.writeToFile(path);//文件夹路径必须存在 可以提前创建PoitlIOUtils.closeQuietlyMulti(template);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());throw new BaseException("Word文档生成失败");} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}
}

生成效果

在这里插入图片描述

DOCX 转 PDF

初始化 XWPFDocument 需要一个输入流,以下是直接使用文件输入流去初始化。

FileInputStream inputStream1 = new FileInputStream("docx文件位置.docx");
XWPFDocument xwpfDocument = new XWPFDocument(inputStream1);
PdfOptions options = PdfOptions.create();
try (OutputStream outPDF = Files.newOutputStream(Paths.get("要生成的pdf位置.pdf"))) {PdfConverter.getInstance().convert(xwpfDocument.getXWPFDocument(), outPDF, options);
} catch (IOException e) {log.error("PDF转换失败",e);
}

PDF 生成效果

在这里插入图片描述

注意

DOCX 模板中如果表格元素中放了图片(如上图中的头像),要确保生成的文件中的图片大小不超过模板中单元格大小。
即图片所在单元格不能被图片撑大,否则图片在转换成PDF时无法展示。

http://www.dinnco.com/news/85099.html

相关文章:

  • 中牟网站建设网络营销中心
  • 事业单位网站建设费科目seo jsbapp9
  • 旅游响应式网站建设国际新闻直播
  • phpweb网站优化网络营销培训课程
  • 哪些网站可以做商家学历提升
  • 怎样为公司做网站重大军事新闻
  • 用jsp sqlserver做的购物网站搜狗网站收录入口
  • 北京门户企业网站建设餐饮品牌全案策划
  • ppt的网站导航栏怎么做自助建站系统源码
  • wordpress网站图片加速百度指数关键词未收录怎么办
  • 做名片制作网站有什么关联词有哪些
  • wordpress 短码按钮seo软件代理
  • 做sns网站需要什么自己做网站需要什么条件
  • 网站建设 杭州游戏推广
  • 织梦网站更新Html百度关键词竞价价格查询
  • 深圳网站建设营销策划安卓优化大师全部版本
  • 怎么做蒙文网站最近的重大新闻
  • 深圳网站设计服务商关键词汇总
  • 开一间网站建设有限公司模板免费下载网站
  • 宁波正规网站seo公司广告网址
  • 地方性网站商城怎么做线上推广有哪些平台效果好
  • 网站百度收录快seo百度首页排名业务
  • 平邑网站定制英语seo什么意思
  • 网站建设基础ppt专业做网站官网
  • 湖州服装网站建设seo优化轻松seo优化排名
  • 网站页面优化方法系统设置友情链接有什么作用
  • 网站域名解绑百度首页 百度一下
  • 流量网站怎么做网站注册搜索引擎的目的是
  • 自适应网站怎么做移动配置域名注册
  • 口碑好的网站建设公司电商网页制作教程