zoukankan      html  css  js  c++  java
  • java导出echart图到excel 多张图片导出

    分三步:

    1.获取echart图的Base64码

    <input type="hidden" name="img" id="img" />

    $('#img').val(myChart.getDataURL("png"));

    2.解码并生成图片

    public void createImg(HttpServletRequest request){
    String uplodapath = "D:/temp/upload";
    //生成图片begin
    String data = request.getParameter("img");
    String imgname = "test.png";
    String filedir = "D:/temp";
    File file = new File(filedir);
    if(!file.exists()){
    file.mkdirs();
    }
    String[] url = data.split(",");
    String u = url[1];
    BASE64Decoder decoder = new BASE64Decoder();
    // 生成图片
    try {
    // Base64解码
    byte[] b = decoder.decodeBuffer(u);
    OutputStream out = new FileOutputStream(new File(filedir+"\" + imgname));
    out.write(b);
    out.flush();
    out.close();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    //图片end
    }

    3.读取图片到excel

    try {
    XSSFWorkbook wb = new XSSFWorkbook();
    //下载图片到excel
    //begin
    BufferedImage bufferImg = null;
    try {
    //创建作图sheet
    XSSFSheet sheet1 = null;
    if(wb.getSheet("echarts")==null){
    sheet1 = wb.createSheet("echarts");
    }else{
    sheet1 = wb.getSheet("echarts");
    }
    //循环读取图片插入到excel
    String filedir = "D:/temp";
    File file = new File(filedir);
    if(file.isDirectory()){
    String[] files = file.list();
    //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
    XSSFDrawing patriarch = sheet1.createDrawingPatriarch();
    int i = 0;
    int rowbegin = 1;
    int rowend = 8;
    for(String _file : files){
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    bufferImg = ImageIO.read(new File(filedir + "\" + _file));
    ImageIO.write(bufferImg, "png", byteArrayOut);
    //anchor主要用于设置图片的属性
    XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255,(short) 1, rowbegin, (short) 5, rowend);
    //插入图片
    patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
    i++;
    rowbegin = i*8+1;
    rowend = rowbegin + 8;
    }
    }
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("test.xls", "GBK"));
    OutputStream out = response.getOutputStream();
    // 写入excel文件
    wb.write(out);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //end
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    参考来源:https://www.cnblogs.com/sun-space/p/5672373.html

    http://blog.csdn.net/qq_33212500/article/details/73274799

  • 相关阅读:
    批量编译当前目录下4gl文件
    oracle数据库查看表
    Oracle中授权(grant)和同义词(synonym)
    Oracle中的instr()函数 详解及应用
    T100的程序错误提示方法
    六种 主流ETL 工具的比较
    oracle恢复数据到某个时间点
    Oracle统计一个小时内,一天内、一个星期内、一个月内、一年内的数据
    Linux top命令的用法详细详解
    Win10操作系统无法访问局域网共享文件夹的问题
  • 原文地址:https://www.cnblogs.com/seeusmile-cnblog/p/7986660.html
Copyright © 2011-2022 走看看