【问题标题】:Generate xlsx file with POI in a zip在 zip 中生成带有 POI 的 xlsx 文件
【发布时间】:2014-10-07 03:49:41
【问题描述】:

我使用 POI Api 将数据导出到 xlsx 文件中,并将它们添加到 Zip 文件中。当我打开 zip 文件时,我没有任何 xlsx 文件,只有三个目录(docProps、xl 和 _rels)和 1 个文件 [Content_Types] xml。我认为是 xlsx 文件的描述,但我不明白为什么。

代码:

public InputStream exportXlsx(List<MyObject> listeOfObject) throws IOException {

        ByteArrayOutputStream excelOutputStreamZip = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(excelOutputStreamZip);

        for (MyObject myObject : listeOfObject) {

            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet wsheet = wb.createSheet("mySheet");
            XSSFRow row = wsheet.createRow(0);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue(myObject.getValue1());

            // Create all sheet and cell....

            // Write WB conntent in outputStream
            wb.write(excelOutputStreamZip);

            addEntry(zip, myObject.getFileName(), excelOutputStreamZip);
        }

        InputStream inputStreamZipByte = new ByteArrayInputStream(
                ((ByteArrayOutputStream) excelOutputStreamZip).toByteArray());
        zip.close();

        return inputStreamZipByte;

    }

    public void addEntry(OutputStream zip, String filename, ByteArrayOutputStream os) {

        byte[] bytes = os.toByteArray();

        ZipEntry entry = new ZipEntry(filename);
        Date d = new Date();
        entry.setTime(d.getTime());
        try {
            ((ZipOutputStream) zip).putNextEntry(entry);
            ((ZipOutputStream) zip).write(bytes);
            ((ZipOutputStream) zip).closeEntry();
        } catch (IOException e) {
            log.error("Can't read the file !", e);
        } catch (ClassCastException cce) {
            log.error("Bad format !", cce);
        }

    }

这段代码写在一个注入到 Struts2 动作中的服务中。

struts.xml:

<action name="*MyAction" class="com.omb.view.action.myAction" method="{1}">
    <result name="export" type="stream">
        <param name="contentType">application/zip</param>
        <param name="inputName">inputStreamZipByte</param>
        <param name="contentDisposition">attachment;filename="myZip.zip"</param> 
        <param name="bufferSize">1024</param>
    </result>
</action>

【问题讨论】:

    标签: java excel struts2


    【解决方案1】:

    我找到了部分解决方案,但现在我遇到了另一个问题:

    初始帖子的问题在于流的处理。因为我对 Zip 和 Workbook 使用了相同的 outputStream。解决方案为每个工作簿创建了一个新的 ByteArrayOutpuStream。

    // Write WB conntent in outputStream    
    ByteArrayOutputStream wbOutputStream = new ByteArrayOutputStream();  
    wb.write(wbOutputStream);    
    addEntry(zip, myObject.getFileName(), wbOutputStream);    
    wbOutputStream.close();
    

    但是...现在生成的 Zip 文件已损坏...

    【讨论】:

    • 如果您想从 Struts2 take a look at this 在 ZIP 中动态添加多个文件(xls、xlsx 等),它可能会有所帮助。
    • 谢谢安德里亚,我的帖子解决了我的问题。
    • 在使用 JSF 和 Spring 时也遇到同样的错误,我也不明白为什么
    【解决方案2】:

    xlsx 文件是一个 zip 文件。您应该检查适合结果的contentType 参数的MIME 类型。见What is a correct mime type for docx, pptx etc?

    【讨论】:

    • 好的,谢谢您的帮助。我试图生成一组 xlsx 文件并将它们放在一个 zip 文件中,供用户下载。那么我应该更改 struts2 配置文件中的 contentType 吗?即使想要一个 zip 文件?
    【解决方案3】:

    只需将工作簿写入 BytearrayInputStream 然后转换为字节数组 在 ZipOutPutStream 中使用该字节数组

    这个对我有用.......................

    response.setContentType("应用程序/zip");

    response.setHeader("Content-Disposition","attachment;filename=\""+fileName+".zip\"");

    workBook = getWorkbook(displayList, cfgType);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ZipOutputStream zos = new ZipOutputStream(baos);

    ByteArrayOutputStream fileBaos = new >ByteArrayOutputStream();

    zos.putNextEntry(new ZipEntry("Test.xlsx"));

    workBook.write(fileBaos);

    zos.write(fileBaos.toByteArray());

    zos.putNextEntry(new ZipEntry("Test1.xlsx"));

    zos.write(fileBaos.toByteArray());

    fileBaos.close(); zos.close();

    response.getOutputStream().write(baos.toByteArray()); response.flushBuffer();

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2012-05-30
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2018-12-30
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多