vue导出带图片xls

版权声明: https://blog.csdn.net/weixin_41722928/article/details/86504075

 1  <v-chart :options="batchCatalogues" style="width: 100%;margin-bottom: 10px" :auto-resize="true" ref="chart"></v-chart>

 2  //导出
      excel({
              search_keys = JSON.parse(this.search_keys_snap),
            }){
        let b = this.$refs.chart.getDataURL('png');
        debugger
        service.installdownloadExcel({
          year : search_keys.year || null,
          type: search_keys.type || null,
          batch: search_keys.batch || null,
          cityAreaCode: search_keys.region && search_keys.region[0] || null,
          countyAreaCode: search_keys.region && search_keys.region[1] || null,
        }, this.$refs.chart.getDataURL('png').split('base64,')[1]).then(resp=>{
          downloadBlobResponse(resp);?//?文件下载
        })
      },

   3 
     //导出
  installdownloadExcel:(params,base)=>
    postRequest(`~/sys/subsidy/excel`,{params,responseType:'blob',data:{base}}),

   4 Java

       /**
     * 导出
     * @return
     */
    @PostMapping("/excel")
    public Object download(@RequestParam Map<String, String> params, @RequestBody Map<String, String> body){
        return service.downloadExcel(params,body);
        
    }
 5 Java
     @Override
    public Object downloadExcel(Map<String, String> map,Map<String, String> body) {
        String year=(String) map.get("year"); 
        String cityAreaCode=(String) map.get("cityAreaCode"); 
        List<Map<String, String>> ms =yearDao.findRegionProducePowerPropInfoForTable(year,cityAreaCode);
        String[] titles = { "地区","总发电量","风电发电量", "风电占比","光伏发电量" , "光伏占比","生物质发电量","生物质占比"};
        return educeExcel(titles, ms,map,body);       }

    /**
     * 
     * @param titles 第一列名
     * @param list 向单元格插入数据
     * @return
     */
    private Object educeExcel(String[] titles,List<Map<String, String>> list,Map<String, String> map,Map<String, String> body){ 
        String base=body.get("base");
         // Base64解码
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes=null;
        try {
            bytes = decoder.decodeBuffer(base);
        } catch (IOException e1) { 
            e1.printStackTrace();
        }
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] < 0) {// 调整异常数据
                bytes[i] += 256;
            }
        } 
        //创建Excel对象
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建工作表单
                HSSFSheet sheet = workbook.createSheet("统计"); 
                // 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                 //anchor主要用于设置图片的属性
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short) 0, 0, (short) 10, 10);   
                //插入图片  
                patriarch.createPicture(anchor, workbook.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG));  
                //CellPoint cellPoint = new CellPoint(0, 1); 
                //创建HSSFRow对象 (行)第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
                int r=12;
                HSSFRow row = sheet.createRow(r);     
                row.setHeightInPoints(20);// 设备标题的高度
                 CellStyle  style =  workbook.createCellStyle();
                 HSSFFont font = workbook.createFont();
                 font.setBold(true);
                 style.setFont(font);
                 style.setFillPattern(FillPatternType.SOLID_FOREGROUND);  
                 style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());    
                //创建HSSFCell对象  (单元格)
                HSSFCell cell=null; 
                //设置第一列单元格的列  
                for(int i = 0; i < titles.length; i++){
                    cell = row.createCell(i);//列索引从0开始
                    cell.setCellValue(titles[i]);//列名1
                    cell.setCellStyle(style);
                };  
                //设置单元格的值  
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow(i+r+1);  
                    Map<String, String> example =list.get(i); 
                    // 第六步,创建单元格,并设置值 
                    String  a1 =  (String) example.get("name"); //地区
                    row.createCell(0).setCellValue(a1); 
                    String a2 =  (String) example.get("allPower"); //总量
                    row.createCell(1).setCellValue(a2); 
                    String a3="";
                    if(example.get("windPower") !=null){ 
                        a3 = (String) example.get("windPower"); //风电
                    }
                    row.createCell(2).setCellValue(a3); 
                    String a4 = (String) example.get("windRate");  //风电占比
                    row.createCell(3).setCellValue(a4); 
                    String a5= (String) example.get("lightPower");  //光伏
                    row.createCell(4).setCellValue(a5); 
                    String a6= (String) example.get("lightRate");  //光伏占比
                    row.createCell(5).setCellValue(a6); 
                    String a7= (String) example.get("bisPower");  //生物质
                    row.createCell(6).setCellValue(a7); 
                    String a8= (String) example.get("bisRate");  //生物质占比
                    row.createCell(7).setCellValue(a8); 
                     
                }
                System.out.println(">>>>>>>>>>>execl>>>>>");
                //输出Excel文件  
                try {
                    workbook.write(os);
                    workbook.close();
                    String filename_enc = UriUtils.encode(
                            "数据导出.xls", "UTF-8");
                      ResponseEntity<byte[]>response = ResponseEntity
                            .ok()
                            .contentType(
                                    MediaType
                                            .parseMediaType("application/octet-stream"))
                            .header("Access-Control-Expose-Headers",
                                    "Content-Disposition")
                            .header("Content-Disposition",
                                    "attachment; filename*=UTF-8''" + filename_enc) // 例子:"attachment;
                            .body(os.toByteArray()); 
                      return response;
                } catch (Exception e) {    
                    throw new ExcelException("服务器忙");
                     
            }    

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/86504075