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

依赖

<!-- word导出 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
        </dependency>
        <!--  上面需要的依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

代码

基础语法

public void aaa() {
        String filePath = "D:\\test\\巡查日志.docx";
        XWPFTemplate template = XWPFTemplate.compile(filePath);
        // 填充数据
        Map<String, Object> data = new HashMap<>();
        data.put("inspectionTime", "(1)第一行\n(2)第二行");
        data.put("deptName", 123);
        // 读取本地磁盘图片
        data.put("weChatPicture", new PictureRenderData(100, 100, "C:\\Users\\Administrator\\Pictures\\16194037861239194.jpg"));
        // 通过url读取网络图片
        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")));
        template.render(data);
        File file = new File("D:\\test\\test1.docx");
        // 保存Word文档
        FileOutputStream out = new FileOutputStream(file);
        template.write(out);
        out.close();
    }

模板使用{{占位符}}

图片{{@占位符}}

换行\n

 

图片和文字遍历使用

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

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

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

文字
public static void main(String[] args) throws IOException {

        String filePath = "D:\\test\\巡查日志.docx";
        XWPFTemplate template = XWPFTemplate.compile(filePath);
        Map<String, Object> map = new HashMap<>();

        List<Map<String,Object>> maps1 = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String,Object> m = new HashMap<>();
            m.put("pho",", 哈哈哈"+i);
            maps1.add(m);
        }

        map.put("photoCollection", maps1);

        template.render(map);

        File file = new File("D:\\test\\test1.docx");
        FileOutputStream out = new FileOutputStream(file);
        template.write(out);
        out.close();
    }
照片

public static void main(String[] args) throws IOException {

        String filePath = "D:\\test\\巡查日志.docx";
        XWPFTemplate template = XWPFTemplate.compile(filePath);
        Map<String, Object> map = new HashMap<>();

        List<Map<String,Object>> maps1 = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String,Object> m = new HashMap<>();
            // 读取本地磁盘图片
            m.put("pho", new PictureRenderData(30, 30, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));
            maps1.add(m);
        }

        map.put("photoCollection", maps1);
        map.put("pho1", new PictureRenderData(100, 100, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));


        template.render(map);

        File file = new File("D:\\test\\test1.docx");
        FileOutputStream out = new FileOutputStream(file);
        template.write(out);
        out.close();
    }

列表的使用

public static void main(String[] args) throws IOException {


        DecimalFormat df = new DecimalFormat("######0.00");
        Calendar now = Calendar.getInstance();
        double money = 0;//总金额
        //组装表格列表数据
        List<Map<String,Object>> detailList=new ArrayList<Map<String,Object>>();
        for (int i = 0; i < 6; i++) {
            Map<String,Object> detailMap = new HashMap<String, Object>();
            detailMap.put("index", i+1);//序号
            detailMap.put("title", "商品"+i);//商品名称
            detailMap.put("product_description", "套");//商品规格
            detailMap.put("buy_num", 3+i);//销售数量
            detailMap.put("saleprice", 100+i);//销售价格

            double saleprice=Double.parseDouble(String.valueOf(100+i));
            int buy_num= Integer.parseInt(String.valueOf(3 + i));
            String buy_price=df.format(saleprice*buy_num);
            detailMap.put("buy_price", buy_price);//单个商品总价格
            money=money+Double.parseDouble(buy_price);

            detailList.add(detailMap);
        }
        //总金额
        String order_money=String.valueOf(money);


        String filePath = "D:\\test\\order12.docx";
        //渲染表格
        HackLoopTableRenderPolicy  policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder().bind("detailList", policy).build();
        Map<String,Object> map = new HashMap<>();

        map.put("detailList", detailList);
        map.put("order_number", "2356346346645");
        map.put("y", now.get(Calendar.YEAR));//当前年
        map.put("m", (now.get(Calendar.MONTH) + 1));//当前月
        map.put("d", now.get(Calendar.DAY_OF_MONTH));//当前日
        map.put("order_money",order_money);//总金额
        XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(map);

        File file = new File("D:\\test\\test1.docx");
        FileOutputStream out = new FileOutputStream(file);
        template.write(out);
        out.close();
    }

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java可以使用第三方工具类库Hutool或者fr.opensagres.xdocreport来根据Word模板导出Word文档docx。其中,使用Hutool可以通过以下步骤实现: 1. 引入Hutool依赖: ``` <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.10</version> </dependency> ``` 2. 加载Word模板文件: ``` DocxWriter writer = new DocxWriter("template.docx"); ``` 3. 替换模板中的变量: ``` writer.replace("name", "张三"); ``` 4. 保存并关闭文件: ``` writer.flush(new FileOutputStream("output.docx")).close(); ``` 而使用fr.opensagres.xdocreport可以通过以下步骤实现: 1. 引入相关依赖: ``` <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.document.docx</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> ``` 2. 加载Word模板文件: ``` InputStream in = new FileInputStream(new File("template.docx"));IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker); ``` 3. 创建数据模型并填充数据: ``` IContext context = report.createContext(); context.put("name", "张三"); ``` 4. 生成Word文档并保存: ``` OutputStream out = new FileOutputStream(new File("output.docx")); report.process(context, out); out.close(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值