Java 使用poi-tl下载Word

poi-tl官网

poi-tl(poi template language)是基于Apache POI的Word模板引擎。纯Java组件,跨平台,代码短小精悍,通过插件机制使其具有高度扩展性。

引入包

compile group: 'com.deepoove', name: 'poi-tl', version: '1.4.2'

定义模板

在这里插入图片描述

word生成代码

public class WordTest {

   /**
     * 准备数据
     */
    @Test
    public void test() {
        String option = "{xAxis: {\n" +
                "        type: 'category',\n" +
                "        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']\n" +
                "    },\n" +
                "    yAxis: {\n" +
                "        type: 'value'\n" +
                "    },\n" +
                "    series: [{\n" +
                "        data: [820, 932, 901, 934, 1290, 1330, 1320],\n" +
                "        type: 'line'\n" +
                "    }]\n" +
                "}";
        String option2=" {\n" +
                "    backgroundColor: '#2c343c',\n" +
                "\n" +
                "    title: {\n" +
                "        text: 'Customized Pie',\n" +
                "        left: 'center',\n" +
                "        top: 20,\n" +
                "        textStyle: {\n" +
                "            color: '#ccc'\n" +
                "        }\n" +
                "    },\n" +
                "\n" +
                "    tooltip : {\n" +
                "        trigger: 'item',\n" +
                "        formatter: \"{a} <br/>{b} : {c} ({d}%)\"\n" +
                "    },\n" +
                "\n" +
                "    visualMap: {\n" +
                "        show: false,\n" +
                "        min: 80,\n" +
                "        max: 600,\n" +
                "        inRange: {\n" +
                "            colorLightness: [0, 1]\n" +
                "        }\n" +
                "    },\n" +
                "    series : [\n" +
                "        {\n" +
                "            name:'访问来源',\n" +
                "            type:'pie',\n" +
                "            radius : '55%',\n" +
                "            center: ['50%', '50%'],\n" +
                "            data:[\n" +
                "                {value:335, name:'直接访问'},\n" +
                "                {value:310, name:'邮件营销'},\n" +
                "                {value:274, name:'联盟广告'},\n" +
                "                {value:235, name:'视频广告'},\n" +
                "                {value:400, name:'搜索引擎'}\n" +
                "            ].sort(function (a, b) { return a.value - b.value; }),\n" +
                "            roseType: 'radius',\n" +
                "            label: {\n" +
                "                normal: {\n" +
                "                    textStyle: {\n" +
                "                        color: 'rgba(255, 255, 255, 0.3)'\n" +
                "                    }\n" +
                "                }\n" +
                "            },\n" +
                "            labelLine: {\n" +
                "                normal: {\n" +
                "                    lineStyle: {\n" +
                "                        color: 'rgba(255, 255, 255, 0.3)'\n" +
                "                    },\n" +
                "                    smooth: 0.2,\n" +
                "                    length: 10,\n" +
                "                    length2: 20\n" +
                "                }\n" +
                "            },\n" +
                "            itemStyle: {\n" +
                "                normal: {\n" +
                "                    color: '#c23531',\n" +
                "                    shadowBlur: 200,\n" +
                "                    shadowColor: 'rgba(0, 0, 0, 0.5)'\n" +
                "                }\n" +
                "            },\n" +
                "\n" +
                "            animationType: 'scale',\n" +
                "            animationEasing: 'elasticOut',\n" +
                "            animationDelay: function (idx) {\n" +
                "                return Math.random() * 200;\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        String txt = "这是一段文字";
        String testImg2 = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3601643726,2993647989&fm=26&gp=0.jpg";
        getWord(option, option2, txt, testImg2);
    }


  /**
     * 生成word
     * @param option   echartsoption
     * @param option2  echartsoption
     * @param txt     文字
     * @param testImg2  网络图片地址
     */
    public static void getWord(String option, String option2, String txt, String testImg2) {
        try {
            //获取echarts图片流
            List<byte[]> listByte = getImgByte(option, option2);
            //获取模板
            Resource resource = new ClassPathResource("template/test.docx");
            Map map = new HashMap();
            map.put("txt", txt);
            map.put("testimg", new PictureRenderData(500, 300, ".png", listByte.get(0)));
            map.put("testimg2", new PictureRenderData(500, 300, ".png", BytePictureUtils.getUrlByteArray(testImg2)));
            map.put("testimg3", new PictureRenderData(500, 300, ".png", listByte.get(1)));
            XWPFTemplate template = XWPFTemplate.compile(resource.getFile()).render(map);
            downloadLocalhost(template);
            return;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 获取多个图片流
     *
     * @return
     */
    public static List<byte[]> getImgByte(String option, String option2) {
        String options[] = {option, option2};
        List<byte[]> listByte = WebDriverUtil.getImgByte(options);
        return listByte;
    }


    /**
     * 网络流下载
     *
     * @param template
     * @param response
     */
    public static void downloadServlet(XWPFTemplate template, HttpServletResponse response) {
        try {
            response.setHeader("Content-disposition", "attachment;filename=" + new String("简报".getBytes("gb2312"), "ISO8859-1") + ".docx");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setContentType("multipart/form-data;charset=UTF-8");
            ServletOutputStream out = response.getOutputStream();
            template.write(out);
            out.flush();
            template.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 下载至本地
     *
     * @param template
     */
    public static File downloadLocalhost(XWPFTemplate template) {
        try {
            File file = new File("D:/简报.docx");
            FileOutputStream out = new FileOutputStream(file);
            template.write(out);
            out.flush();
            out.close();
            template.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

获取echarts图片流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr . zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值