首页 > 基础资料 博客日记

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

2024-02-27 21:00:05基础资料围观325

本篇文章分享Java的XWPFTemplate工具类导出word.docx的使用,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识

依赖

<!-- 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();
    }


文章来源:https://blog.csdn.net/m0_74608954/article/details/132817316
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云