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

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

问题描述

我已经用JSF编写了一个简单的Web应用程序,用于下载Excel报表。我可以在十秒钟之内将至少五行的六行写入五张不同的表格。



当我尝试格式化Excel数据时,问题出现。格式化需要很多时间,重要的是当我尝试使用数据格式化数据超过3000行,并通过JSF下载时,会抛出Zip Bomb IO异常。



因此,我无法计算格式化工作簿的大小(这需要写入byteoutputstream)。



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



是否可以从服务器下载完整格式的Excel,其中有五行五行?



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



对于标题行:

  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;
}

对于列/单元间距:


$ b $ int code $ for(int cellCount = 0; cellCount< row.getLastCellNum
row.getCell(cellCount).setCellStyle(cellStyleHeader);
}


解决方案

Zip-Bomb检测尝试保护您不要打开恶意文件,试图将您的应用程序从内存中释放出来。



在您的情况下,它似乎正在踢,因为SXSSFWorkbook内部将数据流输出到临时文件,然后再次读取它以生成最终的.xlsx文件。



如果您确定文档以安全的方式构建,那么可以增加或者甚至使用错误消息中包含的指令来禁用这些检查。


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.

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?

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

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

For Header row:

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;
    }

For data rows:

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 column/cell spacing:

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

解决方案

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

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天全站免登陆