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'

定义模板

java使用poi-tl下载Word_第1张图片

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} 
{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 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 getImgByte(String option, String option2) { String options[] = {option, option2}; List 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; } }

效果

java使用poi-tl下载Word_第2张图片
java使用poi-tl下载Word_第3张图片
java使用poi-tl下载Word_第4张图片

获取echarts图片流

你可能感兴趣的:(springboot)