编写大格式 Excel (.xlsx) 时出现 Zip-bomb 异常 [英] Zip-bomb exception while writing a large formatted Excel (.xlsx)

查看:25
本文介绍了编写大格式 Excel (.xlsx) 时出现 Zip-bomb 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 JSF 编写了一个简单的 Web 应用程序,用于下载 Excel 报告.我能够在不到 10 秒的时间内将至少 50,000 行包含六列的内容写入五个不同的工作表上.

I've written a simple web app with JSF which is used to download Excel reports. I'm able to write to at least 50,000 rows with six columns onto five different sheets in under ten seconds.

当我尝试格式化 Excel 数据时出现问题.格式化需要相当长的时间,重要的是当我尝试使用超过 3,000 行的数据格式化 Excel 并通过 JSF 下载时,它会引发 Zip Bomb IO 异常.

The problem comes when I try to format the Excel data. The formatting takes quite sometime and important thing is when I try to format Excel with data more than 3,000 rows and download through the JSF, it throws the Zip Bomb IO exception.

因此,我也无法计算格式化工作簿的大小(这需要写入字节输出流).

Due to this, I'm unable to compute the size of the formatted workbook also (which requires writing to a byteoutputstream).

有人可以对此提供任何见解吗?

Can anybody provide any insight on this?

是否可以从服务器下载一个完全格式化的 Excel,它在五张纸中有 50K 行?

Is it possible to download from the server a fully formatted Excel which has 50K rows in five sheets?

以下是我用于格式化 Excel 行的代码.

Below is the code I use for formatting the Excel rows.

对于标题行:

public CellStyle formatHeaderRow(SXSSFWorkbook sxWorkBook){
        CellStyle cellStyleHeader = sxWorkBook.createCellStyle();
        Font font = sxWorkBook.createFont();
        font.setFontHeightInPoints((short)10);
        font.setFontName("Arial");
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyleHeader.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cellStyleHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cellStyleHeader.setBorderTop((short) 1);
        cellStyleHeader.setBorderLeft((short) 1);
        cellStyleHeader.setBorderRight((short) 1);
        cellStyleHeader.setBorderBottom((short) 1);
        cellStyleHeader.setAlignment((short) CellStyle.ALIGN_CENTER);
        cellStyleHeader.setFont(font);
        return cellStyleHeader;
    }

对于数据行:

public CellStyle formatBodyRows(SXSSFWorkbook sxWorkBook){
        CellStyle cellStyleBody =  sxWorkBook.createCellStyle();
        Font bodyFont = sxWorkBook.createFont();
        bodyFont.setFontHeightInPoints((short)10);
        bodyFont.setFontName("Arial");
        cellStyleBody.setBorderTop((short) 1);
        cellStyleBody.setBorderLeft((short) 1);
        cellStyleBody.setBorderRight((short) 1);
        cellStyleBody.setBorderBottom((short) 1);
        cellStyleBody.setAlignment((short) CellStyle.ALIGN_LEFT);
        cellStyleBody.setFont(bodyFont);
        return cellStyleBody;
    }

对于列/单元格间距:

for(int cellCount=0;cellCount<row.getLastCellNum();cellCount++){
            sheet.setColumnWidth(cellCount, width);
            row.getCell(cellCount).setCellStyle(cellStyleHeader);
        }

推荐答案

Zip-Bomb 检测会尝试保护您免于打开恶意文件,这些文件会导致您的应用程序内存不足.

The Zip-Bomb detection tries to protect you from opening malicious files which try to bring your application out of memory.

这似乎适用于您的情况,因为 SXSSFWorkbook 在内部将数据流式传输到临时文件,然后再次读取它以生成最终的 .xlsx 文件.

It seems it kicks in in your case because the SXSSFWorkbook internally streams out the data to a temp-file and then reads it in again to produce the final .xlsx file.

如果您确定此处的文档是以安全的方式构建的,那么您可以使用错误消息中包含的说明增加甚至禁用这些检查.

If you are sure the document is built in a safe way here, then you can increase or even disable these checks with the instructions contained in the error message.

这篇关于编写大格式 Excel (.xlsx) 时出现 Zip-bomb 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆