• poi-tl word模版生成、动态表格、坑点合集


    一、配置

    1、导入依赖

    1. com.deepoove
    2. poi-tl
    3. 1.10.0

    apache poi版本要对应 

    1. org.apache.poi
    2. poi
    3. 4.1.2
    4. org.apache.poi
    5. poi-ooxml
    6. 4.1.2
    7. org.apache.poi
    8. poi-ooxml-schemas
    9. 4.1.2

    2、模版存放问题

    (1)如果你的模版存放在你c盘之类的,是不用再去配置maven文件

    (2)如果你的是要发布到inux的spring项目,或者为了可用性,将模版存放在resource文件夹下面,这个时候就要配置maven文件,因为打jar包的时候,会将你的word文件默认压缩,通过maven构建文件不对后缀docx的文件压缩过滤,这样文件打包之后就不会损坏

    1. org.apache.maven.plugins
    2. 2.6
    3. maven-resources-plugin
    4. UTF-8
    5. docx

    二、案例

    1、普通模版

    新建一个word文档,{{}}这种格式,里面放替换的字母,将map集合替换内容设置好,交给poi-tl渲染。

    word模版

    以下就是普通模版生成的三个方法,其中一个为主方法,调用生成。如果需要字节流转成mutipartfile需要导入mock依赖

    1. org.springframework
    2. spring-mock
    3. 2.0.8
    1. public class WordTemplateUtils {
    2. public static void main(String[] args) {
    3. Map templateMap = new HashMap<>();
    4. templateMap.put("Year", "2023年");
    5. templateMap.put("HqCompany","测试名单");
    6. exportWord(templateMap);
    7. }
    8. /**
    9. * 生成word模版
    10. *
    11. * @param data
    12. * @return
    13. */
    14. public static MultipartFile exportWord(Map data) {
    15. MultipartFile multipartFile = null;
    16. ByteArrayOutputStream ostream = null;
    17. XWPFTemplate template = null;
    18. try {
    19. //获取模板
    20. template = getWordTemplate(data);
    21. ostream = new ByteArrayOutputStream();
    22. // 将模板输出到字节流
    23. template.write(ostream);//这里还有一个方法是template.writeToFile("");可以直接填写本地文件路径
    24. byte[] fileBytes = ostream.toByteArray();
    25. String newFileName = "测试" + data.get("Year") + "年生成文件.docx";
    26. //转成可上传的文件(作者这里是为了上传到服务器转换成mutipartFile)
    27. multipartFile = new MockMultipartFile(newFileName, newFileName,
    28. "application/vnd.openxmlformats-officedocument.wordprocessingml.document", fileBytes);
    29. } catch (IOException e) {
    30. log.error("模版1导出异常");
    31. } finally {
    32. //关闭流
    33. IOUtils.close(ostream);
    34. IOUtils.close(template);
    35. }
    36. return multipartFile;
    37. }
    38. /**
    39. * 获取普通word模版渲染
    40. *
    41. * @param data
    42. * @return
    43. * @throws IOException
    44. */
    45. private static XWPFTemplate getWordTemplate(Map data) {
    46. //获取模板的输入流,读取resource里面的template文件夹里的word1.docx
    47. ClassPathResource tempFileResource = new ClassPathResource("template/" + "word1.docx");
    48. InputStream stream = null;
    49. try {
    50. stream = tempFileResource.getInputStream();
    51. } catch (IOException e) {
    52. log.error("获取模版失败!");
    53. }
    54. ConfigureBuilder builder = Configure.builder();
    55. builder.useSpringEL();
    56. XWPFTemplate template = XWPFTemplate.compile(stream, builder.build()).render(data);
    57. return template;
    58. }
    59. }

    2、带有表格的模版

    word模版

     代码:

    1. public class WordTemplateUtils {
    2. /**
    3. * 生成word表格模版
    4. *
    5. * @param data
    6. * @return
    7. */
    8. public static MultipartFile exportWordTable(Map data) {
    9. //模板地址
    10. ClassPathResource tempFileResource = new ClassPathResource("template/" + "word2.docx");
    11. // 行循环实例
    12. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
    13. //这里可以指定一个config类,用来指定一些规则,也可以改变模板中{{}}的这种格式
    14. MultipartFile multipartFile = null;
    15. XWPFTemplate compile = null;
    16. File file = null;
    17. ByteArrayOutputStream ostream = null;
    18. try {
    19. //lists绑定对象
    20. Configure config = Configure.builder()
    21. .bind("lists", policy).build();
    22. compile = XWPFTemplate.compile(tempFileResource.getInputStream(), config);
    23. compile.render(data);
    24. ostream = new ByteArrayOutputStream();
    25. compile.write(ostream);
    26. byte[] fileBytes = ostream.toByteArray();
    27. String newFileName = "测试" + data.get("Year") + "年企业名单.docx";
    28. multipartFile = new MockMultipartFile(newFileName, newFileName,
    29. "application/vnd.openxmlformats-officedocument.wordprocessingml.document", fileBytes);
    30. } catch (IOException e) {
    31. log.error("模版导出失败");
    32. } finally {
    33. IOUtils.close(ostream);
    34. IOUtils.close(compile);
    35. }
    36. return multipartFile;
    37. }
    38. public static void main(String[] args) {
    39. Map templateMap = new HashMap<>();
    40. //建立公司对象集合
    41. List companyList=new ArrayList();
    42. Company company1=new Company();
    43. company1.setIndex(1);
    44. company1.setEnterpriseName("测试1");
    45. Company company2=new Company();
    46. company1.setIndex(2);
    47. company1.setEnterpriseName("测试2");
    48. companyList.add(company1);
    49. //放进模版集合
    50. templateMap.put("Year", "2023年");
    51. templateMap.put("lists","测试名单");
    52. exportWordTable(templateMap);
    53. }
    54. }

  • 相关阅读:
    python机器人编程——垃圾自动分类,在VREP环境中,UARM与摄像头联动,实现基于视觉识别的自动抓取(下)
    Android11 Launcher添加网格布局设置(3x3,4x4,5x5)
    node_modules/node-sass npm ERR! command failed解决方法
    电力调度自动化系统,如何减少配电安全隐患?
    J9数字货币论:什么是区块链节点
    mac 备忘录
    postgresql|数据库|数据迁移神器ora2pg的安装部署和初步使用
    vue判断图片是否可用访问,不能访问就重新获取
    Javascript实现继承的几种方式
    2022薪酬调查结果,CRISC和CDPSE更是包揽了冠亚军
  • 原文地址:https://blog.csdn.net/weixin_43162044/article/details/132803538