zoukankan      html  css  js  c++  java
  • java将HSSFWorkbook生成的excel压缩到zip中

    思路:1.写入输入流中。

    2.将输入流加到ZipOutputStream压缩流中

    List<DocumentModel> list = null;

    try {
    list = documentService.exportDataList1(idsn);
    } catch (Exception e) {
    e.printStackTrace();
    }
    if(list==null||list.size()==0){
    return ReponseResult.error(new CodeMsg(-1, "列表为空!"));
    }

    //excel表格
    HSSFWorkbook wb = documentService.exportBatch1(list);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
    wb.write(os);
    } catch (IOException e) {
    e.printStackTrace();
    }
    byte[] content = os.toByteArray();

    //写入输入流
    InputStream is = new ByteArrayInputStream(content);

    // 创建临时文件
    File zipFile = File.createTempFile(fileName, ".zip");
    FileOutputStream f = new FileOutputStream(zipFile);
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    // 用于将数据压缩成Zip文件格式
    ZipOutputStream zos = new ZipOutputStream(csum);
    /**
    * 添加excel表格数据
    */
    zos.putNextEntry(new ZipEntry("file.xls"));
    int bytesRead = 0;
    while ((bytesRead = is.read()) != -1) {
    zos.write(bytesRead);
    }
    isclose();
    zos.closeEntry();

  • 相关阅读:
    JS写游戏
    为运算表达式设计优先级
    原子的数量
    二叉搜索树的范围和
    所有可能的满二叉树
    有效的井字游戏
    站在对象模型的尖端
    执行期语意学
    构造、析构、拷贝语意学
    [CSP-S模拟测试]:序列(二分答案+树状数组)
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11430100.html
Copyright © 2011-2022 走看看