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

版权声明:本文为博主作者:小星星o原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/m0_74608954/article/details/132817316

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2024年4月16日
下一篇 2024年4月16日

相关推荐