首页 > 技术文章 > echarts的下载为excel小工具

many-object 2017-09-08 11:15 原文

  echarts自带有下载为图片的功能,但是没有下载为excel表格的功能,个人觉得下载为表格更为需要,所以写了个工具,此工具应用有局限性,只能用于柱形图和折线图,option的series要是如下形式:

  

      series: [
                {
                    name: '初级人才',
                    type: 'bar',
                    data: [['部门1',1500], ['部门2',1500], ['部门3',1500], ['部门4',1500], ['部门5',1500], ['部门6',1500], ['部门7',1500], ['部门8',1500], ['部门9',1500], ['部门11',1500], ['部门12',1500], ['部门13',1500], ['部门14',1500], ['部门15',1500]]
                },
                {
                    name: '中级人才',
                    type: 'bar',
                    data: [['部门1',1500], ['部门2',1500], ['部门3',1500], ['部门4',1500], ['部门5',1500], ['部门6',1500], ['部门7',1500], ['部门8',1500], ['部门9',1500], ['部门11',1500], ['部门12',1500], ['部门13',1500], ['部门14',1500], ['部门15',1500]]                },
                {
                    name: '高级人才',
                    type: 'bar',
                    data: [['部门1',1500], ['部门2',1500], ['部门3',1500], ['部门4',1500], ['部门5',1500], ['部门6',1500], ['部门7',1500], ['部门8',1500], ['部门9',1500], ['部门11',1500], ['部门12',1500], ['部门13',1500], ['部门14',1500], ['部门15',1500]]
                }
            ]

  该工具主要是利用series来生成excel所以对series的格式有一定要求。

  在toolbox的feature属性中增加该工具配置:

  

          /*自定义工具,下载为xls*/
                    myExcl:{
                        show:true,
                        title:'下载为excel',
                        icon:'path://M877.6 289.5L656 68.4c-2.8-2.8-6.6-4.4-10.6-4.4h-463c-22.3 0-40.5 18.1-40.5 40.4v815.2c0 22.3 18.1 40.4 40.5 40.4h659.1c22.3 0 40.5-18.1 40.5-40.4V300.1c0-4-1.6-7.8-4.4-10.6zM641.2 96.1l207.6 207.2H641.2V96.1zM841.5 930h-659c-5.8 0-10.5-4.7-10.5-10.4V104.4c0-5.6 4.8-10.4 10.5-10.4h428.8v224.3c0 8.3 6.7 15 15 15H852v586.3c0 5.7-4.7 10.4-10.5 10.4z M447.9 520.2h-32l-44.8 74.2c-3.3 5.7-6 11-8.3 16.1h-0.6c-2.5-5.9-5-11-7.6-15.4l-42.8-74.9h-34.3l66.3 107.7-72.1 108.6H306l49.8-81.3c3.2-6.8 4.8-10.6 5-11.3h0.7c2.1 5.6 3.8 9.3 4.9 11.2l48.4 81.4h34l-68.7-109.1 67.8-107.2zM509 520.2h-28v216.3h114.4v-24.7H509zM693.9 616.5c-19.3-9.5-31.9-17-37.6-22.5-5.8-5.5-8.7-12.6-8.7-21.4 0-9.9 4.2-17.6 12.5-23.1s19.1-8.3 32.4-8.3c20 0 36.5 4.5 49.5 13.5v-30.4c-10.1-5.2-25.9-7.8-47.4-7.8-22 0-40.3 5.3-54.7 15.8-14.5 10.5-21.7 24.8-21.7 42.7 0 11.9 3.5 22.5 10.4 31.8s21.2 19.3 43 30.1c15.7 7.7 26.4 13.5 32 17.2 5.7 3.7 10.2 8 13.4 12.8 3.3 4.8 4.9 10.3 4.9 16.3 0 21.6-15.2 32.4-45.6 32.4-10 0-20.5-1.9-31.6-5.6-11.1-3.7-20-8.5-26.6-14.2v31.8c5.2 3.5 13.5 6.5 25 8.9 11.4 2.4 21.4 3.6 30 3.6 24.5 0 43.7-5.1 57.5-15.2 13.8-10.1 20.7-24.7 20.7-43.7 0-12.7-4.2-24.1-12.7-34.2-8.6-10.2-23.4-20.3-44.7-30.5z',
                        onclick:function (data) {
                            var $form=document.createElement("form");
                            var $input=document.createElement("input");
                            $form.method="post";
                            $form.action="down.do"
                            $form.style="display:none;"
                            $input.name='data';
                            $input.type="textarea";
                            $input.value=JSONUtil.toJson(data.getOption().series);//将series转成JSON字符串
                            $form.append($input);
                            $("body").append($form)
                            $form.submit();
                            $form.remove();
                        }
                    }

        后台如下写:

   

/**
     *  @Title down
     *  @Descript :下载统计数据
     *  @date : 2017-09-05  15:26:39
     *  @param
     *  @return java.lang.String
     *  @throws
     *  @version V1.0
     */
    @RequestMapping("down.do")
    public String down(HttpServletRequest request , HttpServletResponse response) throws IOException {
        List list=getJsonStrToList(request.getParameter("data"));
        response.setHeader("Content-Disposition",
                "attachment;filename=" +
                        new String( ("人员统计" + ".xls").getBytes(),
                                "iso-8859-1"));
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook=echartsToExcel(list);
        workbook.write(response.getOutputStream());
        return null;
    }

/**
     *  @Title echartsToExcel
     *  @Descript :柱形echarts转成excel
     *  @date : 2017-09-07  22:20:45
     *  @param
     *  @return org.apache.poi.hssf.usermodel.HSSFWorkbook
     *  @throws
     *  @version V1.0
     */
    private HSSFWorkbook echartsToExcel(List list){
        HSSFWorkbook workbook=new HSSFWorkbook();
        HSSFSheet sheet=workbook.createSheet("人员统计");
        HSSFRow row;
        HSSFCell cell;
        HSSFCell fCell;
        HSSFRow fRow;
        List data;
        List listTemp;
        Map map;
        fRow=sheet.createRow(0);
        for(int i=0;i<list.size();i++){
            map=(Map)list.get(i);
            row=sheet.createRow(i+1);
            cell=row.createCell(0);
            if(fRow.getCell(0)==null){
                fCell=fRow.createCell(0);
                fCell.setCellValue("");
            }
            cell.setCellValue(map.get("name").toString());
            data=ValidateUtil.isEmpty(map.get("data"))?null:(List)map.get("data");
            if(ValidateUtil.isEmpty(data))continue;
            for(int j =0;j<data.size();j++){
                listTemp=ValidateUtil.isEmpty(data.get(j))?null: (List) data.get(j);
                if(ValidateUtil.isEmpty(listTemp))continue;
                if(fRow.getCell(j+1)==null){
                    fCell=fRow.createCell(j+1);
                    fCell.setCellValue(listTemp.get(0).toString());
                }
                cell=row.createCell(j+1);
                cell.setCellValue(Long.parseLong(listTemp.get(1).toString()));
            }
        }
        return workbook;
    }

 

推荐阅读