zoukankan      html  css  js  c++  java
  • echarts 导出excel

    public ResponseEntity<byte[]> chartExport(HttpServletRequest request, String picinfo, HttpServletResponse response) {
    //类型
    String type = request.getParameter("type");

    try {
    //decodeBase64();
    List<MockTestNetTrend> dataList = JsonUtils.json2List(request.getParameter("mockTestNetTrendList"), MockTestNetTrend.class);
    List<String> cellList = JsonUtils.json2List(request.getParameter("cellList"), String.class);
    // 生成图片
    byte[] picInfoByte = base64TObyte(request, picinfo);
    /* String picPath = "C:\Users\Administrator\Desktop\pic" + ".png";
    File picFile = new File(picPath);//图片文件
    OutputStream picStream = new FileOutputStream(picFile);//图片输出流
    picStream.write(picInfoByte);
    picStream.flush();
    picStream.close();*/


    String outFileName = FileNameUtil.createFileName("xls");
    String exportFileName = type + ".xls";
    //导出路径
    String exportFilePath = CacheUtil.getSysParam(GlobalConstant.COM_PROP_ACCESS_EXPORTPATH);

    // 模板输入流
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();
    int index = 1;

    //创建表头
    createHeader(wb, sheet,cellList);

    //写入数据
    if (!StringUtil.isEmptyList(dataList)) {
    writeData(dataList, wb, sheet, index, type);
    }

    Drawing patri = sheet.createDrawingPatriarch();
    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
    (short) 0, dataList.size() + 2, (short) 5, 25);

    //图通过流的形式插入到excel模板中
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // 将图片写入流中
    ByteArrayInputStream in = new ByteArrayInputStream(picInfoByte); //将picInfoByte作为输入流;
    BufferedImage bufferImg = ImageIO.read(in); //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
    ImageIO.write(bufferImg, "png", outStream); // 利用HSSFPatriarch将图片写入EXCEL

    patri.createPicture(anchor, wb.addPicture(
    outStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
    File outFile = new File(exportFilePath + File.separator + outFileName);
    if (!outFile.getParentFile().exists()) {
    outFile.getParentFile().mkdirs();
    }
    wb.write(new FileOutputStream(outFile));
    outStream.flush();
    outStream.close();
    /* if (picFile.exists()){
    picFile.delete();
    }*/


    return ExportUtil.export(exportFileName, outFile, true);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return null;
    }
  • 相关阅读:
    第一章初识java
    初识jQuery
    初识jQuery的案例
    jdk的安装与环境变量的配置
    第六章连接和分组查询
    第五章模糊查询和聚合函数
    第四章数据查询基础
    第三章用sql语句操作数据
    第二章用表组织数据
    第一章数据库系统基础
  • 原文地址:https://www.cnblogs.com/ljsn/p/12792940.html
Copyright © 2011-2022 走看看