• Java的XWPFTemplate工具类导出word.docx的使用


    依赖

    1. <dependency>
    2. <groupId>com.deepoovegroupId>
    3. <artifactId>poi-tlartifactId>
    4. <version>1.7.3version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.apache.poigroupId>
    8. <artifactId>poi-ooxmlartifactId>
    9. <version>4.1.2version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.apache.poigroupId>
    13. <artifactId>poi-ooxml-schemasartifactId>
    14. <version>4.1.2version>
    15. dependency>
    16. <dependency>
    17. <groupId>org.apache.poigroupId>
    18. <artifactId>poiartifactId>
    19. <version>4.1.2version>
    20. dependency>

    代码

    基础语法

    1. public void aaa() {
    2. String filePath = "D:\\test\\巡查日志.docx";
    3. XWPFTemplate template = XWPFTemplate.compile(filePath);
    4. // 填充数据
    5. Map data = new HashMap<>();
    6. data.put("inspectionTime", "(1)第一行\n(2)第二行");
    7. data.put("deptName", 123);
    8. // 读取本地磁盘图片
    9. data.put("weChatPicture", new PictureRenderData(100, 100, "C:\\Users\\Administrator\\Pictures\\16194037861239194.jpg"));
    10. // 通过url读取网络图片
    11. data.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
    12. template.render(data);
    13. File file = new File("D:\\test\\test1.docx");
    14. // 保存Word文档
    15. FileOutputStream out = new FileOutputStream(file);
    16. template.write(out);
    17. out.close();
    18. }

    模板使用{{占位符}}

    图片{{@占位符}}

    换行\n

     
    

    图片和文字遍历使用

    读取文字:{{?photoCollection}}{{pho}}{{/photoCollection}}

    读取照片:{{?photoCollection}} {@{pho}} {{/photoCollection}}

    特别注意:读取照片的时候需要有一个空格才会显示,找了好久的问题,最后加一个空格解决了

    1. 文字
    2. public static void main(String[] args) throws IOException {
    3. String filePath = "D:\\test\\巡查日志.docx";
    4. XWPFTemplate template = XWPFTemplate.compile(filePath);
    5. Map<String, Object> map = new HashMap<>();
    6. List<Map<String,Object>> maps1 = new ArrayList<>();
    7. for (int i = 1; i <= 5; i++) {
    8. Map<String,Object> m = new HashMap<>();
    9. m.put("pho",", 哈哈哈"+i);
    10. maps1.add(m);
    11. }
    12. map.put("photoCollection", maps1);
    13. template.render(map);
    14. File file = new File("D:\\test\\test1.docx");
    15. FileOutputStream out = new FileOutputStream(file);
    16. template.write(out);
    17. out.close();
    18. }
    19. 照片
    20. public static void main(String[] args) throws IOException {
    21. String filePath = "D:\\test\\巡查日志.docx";
    22. XWPFTemplate template = XWPFTemplate.compile(filePath);
    23. Map<String, Object> map = new HashMap<>();
    24. List<Map<String,Object>> maps1 = new ArrayList<>();
    25. for (int i = 1; i <= 5; i++) {
    26. Map<String,Object> m = new HashMap<>();
    27. // 读取本地磁盘图片
    28. m.put("pho", new PictureRenderData(30, 30, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));
    29. maps1.add(m);
    30. }
    31. map.put("photoCollection", maps1);
    32. map.put("pho1", new PictureRenderData(100, 100, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));
    33. template.render(map);
    34. File file = new File("D:\\test\\test1.docx");
    35. FileOutputStream out = new FileOutputStream(file);
    36. template.write(out);
    37. out.close();
    38. }

    列表的使用

    1. public static void main(String[] args) throws IOException {
    2. DecimalFormat df = new DecimalFormat("######0.00");
    3. Calendar now = Calendar.getInstance();
    4. double money = 0;//总金额
    5. //组装表格列表数据
    6. List<Map<String,Object>> detailList=new ArrayList>();
    7. for (int i = 0; i < 6; i++) {
    8. Map<String,Object> detailMap = new HashMap<String, Object>();
    9. detailMap.put("index", i+1);//序号
    10. detailMap.put("title", "商品"+i);//商品名称
    11. detailMap.put("product_description", "套");//商品规格
    12. detailMap.put("buy_num", 3+i);//销售数量
    13. detailMap.put("saleprice", 100+i);//销售价格
    14. double saleprice=Double.parseDouble(String.valueOf(100+i));
    15. int buy_num= Integer.parseInt(String.valueOf(3 + i));
    16. String buy_price=df.format(saleprice*buy_num);
    17. detailMap.put("buy_price", buy_price);//单个商品总价格
    18. money=money+Double.parseDouble(buy_price);
    19. detailList.add(detailMap);
    20. }
    21. //总金额
    22. String order_money=String.valueOf(money);
    23. String filePath = "D:\\test\\order12.docx";
    24. //渲染表格
    25. HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
    26. Configure config = Configure.newBuilder().bind("detailList", policy).build();
    27. Map<String,Object> map = new HashMap<>();
    28. map.put("detailList", detailList);
    29. map.put("order_number", "2356346346645");
    30. map.put("y", now.get(Calendar.YEAR));//当前年
    31. map.put("m", (now.get(Calendar.MONTH) + 1));//当前月
    32. map.put("d", now.get(Calendar.DAY_OF_MONTH));//当前日
    33. map.put("order_money",order_money);//总金额
    34. XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(map);
    35. File file = new File("D:\\test\\test1.docx");
    36. FileOutputStream out = new FileOutputStream(file);
    37. template.write(out);
    38. out.close();
    39. }

  • 相关阅读:
    【Jenkins+K8s】持续集成与交付 (二十):K8s集群通过Deployment方式部署安装Jenkins
    wifi ping延时大的问题
    音视频云架构
    Python的常用排序算法实现
    Freeswitch操作基本配置
    Day720. 外部内存接口 -Java8后最重要新特性
    ansible - Role
    Azure Kubernetes(AKS)部署及查看应用资源
    软件需求说明书(GB856T-88)
    2022/08/26 day11:高级数据类型
  • 原文地址:https://blog.csdn.net/m0_74608954/article/details/132817316