简体   繁体   English

Apache POI生成的xlsx文件大小大于通过Microsoft Excel手动创建的文件大小

[英]Apache POI generated xlsx file size is larger than manually created via Microsoft Excel

I am using Apache POI to generate xlsx sheet for reports. 我正在使用Apache POI生成报告的xlsx工作表。 One of the POI generated report I saved as another using Microsoft excel . 我使用Microsoft excel将POI生成的报告之一保存为另一报告。 When comparing the original file and the saved file there was 12Mb difference. 比较原始文件和保存的文件时,存在12Mb的差异。 The original file was 15Mb while the saved file is just 2.5Mb. 原始文件为15Mb,而保存的文件仅为2.5Mb。 The Workbook used is XSSFWorkbook. 使用的工作簿是XSSFWorkbook。

Is it possible to reduce the file size created by Apache POI 是否可以减少由Apache POI创建的文件大小

Here is the code snippet I have used: 这是我使用的代码片段:

XSSFWorkbook workbookTitle = new XSSFWorkbook(fileInputStream);
workbook = new SXSSFWorkbook(workbookTitle, maxRows);

font = workbook.createFont();
font.setFontHeightInPoints((short) 9);
font.setFontName(FONT_NAME);

cellTwoDecimal = workbook.createCellStyle();

DataFormat format = workbook.createDataFormat();

cellTwoDecimal.setDataFormat(format.getFormat("0.00"));
cellTwoDecimal.setFont(font);

cellCommon = workbook.createCellStyle();
cellCommon.setFont(font);

cellText = workbook.createCellStyle();
cellText.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text"));
cellText.setFont(font);

cellWrpText = workbook.createCellStyle();
cellWrpText.setWrapText(true);
cellWrpText.setFont(font);


Row row;
Cell cell;

for (int i = 0; i < size; i++) {
    row = excelSheet.createRow(rowIndex++);
    cell = row.createCell(i);
    cell.setCellValue(rowHeader);
    cell.setCellStyle(cellCommon);

}

I have removed some internal logics from code. 我从代码中删除了一些内部逻辑。 Please share your ideas. 请分享您的想法。

[Edit 1] I am inserting a lot of blank cells where there is no value, ie. [编辑1]我要在没有值的地方插入很多空白单元格,即。 some part of the report will not have any value. 该报告的某些部分将没有任何价值。 So I put a blank cell there. 所以我在那儿放了一个空白单元格。 I am also setting style for the blank cell. 我还在为空白单元格设置样式。 Can this be the reason? 这可能是原因吗?

Thanks in advance. 提前致谢。

According to your "edit 1"... if i understand you correctly you create cells with no value. 根据您的“编辑1” ...如果我正确理解您的意见,那么您将创建无价值的单元格。 you do not have to do so. 您不必这样做。 if you dont want to write something then do not create the empty cell. 如果您不想写东西,请不要创建空单元格。 on my poi-experience you only have to create rows and cells if you want to write something. 根据我的poi经验,如果您要编写某些内容,则只需创建行和单元格。

from this point of view it is clear, that your xlsx is very large (many many cell-objects). 从这个角度来看,很明显,您的xlsx非常大(许多单元对象)。 i think MS Excel removes the empty cells on manual save. 我认为MS Excel会在手动保存时删除空单元格。

added: Need to mention that there is also an issue with styling your cells. 补充:需要指出的是,单元格样式也存在问题。 please try to use as few as possible instances of CellStyle. 请尝试使用尽可能少的CellStyle实例。 if you have cells with same style do not create a new instance of CellStyle with same attributes. 如果您具有相同样式的单元格,请不要创建具有相同属性的CellStyle的新实例。 please apply the same instance of CellStyle. 请应用相同的CellStyle实例。 Also do not assign style to simple text cells. 也不要将样式分配给简单的文本单元格。 in this case excel uses a default style (background='white', textcolor='black', font='any default', size='any default', format='default'). 在这种情况下,excel使用默认样式(background ='white',textcolor ='black',font ='any default',size ='any default',format ='default')。

I had a similar problem, and later figured out that I was opening the FileOutputStream in append mode(append=true). 我有一个类似的问题,后来发现我以追加模式(append = true)打开FileOutputStream。 The file size grew exponentially(say from 7KB to 54KB) every-time I update a single cell on the sheet. 每次我更新工作表上的单个单元格时,文件大小都会成倍增长(例如,从7KB增加到54KB)。 When removed the append, it worked just fine. 当删除附件时,它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM