From e4512b8d5ad3873b5dbc6c2e2e39465f4c621768 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 29 Sep 2012 21:36:27 +0000 Subject: [PATCH] Have HSSFOptimiser also remove un-used cell styles, in addition to duplicates git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1391891 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/usermodel/HSSFOptimiser.java | 29 ++++++++++-- .../poi/hssf/usermodel/TestHSSFOptimiser.java | 44 ++++++++++++++++++- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index f8cfdb92b5..99234ca012 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + HSSFOptimiser will now also tidy away un-used cell styles, in addition to duplicate styles 53493 - Fixed memory and temporary file leak in SXSSF 53780 - Fixed memory and temporary file leak in SXSSF 53380 - ArrayIndexOutOfBounds Excetion parsing word 97 document. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java index 50489ef24b..e8768dc2ac 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java @@ -165,7 +165,7 @@ public class HSSFOptimiser { /** * Goes through the Wokrbook, optimising the cell styles - * by removing duplicate ones. + * by removing duplicate ones, and ones that aren't used. * For best results, optimise the fonts via a call to * {@link #optimiseFonts(HSSFWorkbook)} first. * @param workbook The workbook in which to optimise the cell styles @@ -175,8 +175,10 @@ public class HSSFOptimiser { // delete the record for it. Start off with no change short[] newPos = new short[workbook.getWorkbook().getNumExFormats()]; + boolean[] isUsed = new boolean[newPos.length]; boolean[] zapRecords = new boolean[newPos.length]; for(int i=0; i 22 assertEquals(22, r.getCell(7).getCellValueRecord().getXFIndex()); + + + // Add a new duplicate, and two that aren't used + HSSFCellStyle csD = wb.createCellStyle(); + csD.setFont(f1); + r.createCell(8).setCellStyle(csD); + + HSSFFont f3 = wb.createFont(); + f3.setFontHeight((short) 23); + f3.setFontName("Testing 3"); + HSSFFont f4 = wb.createFont(); + f4.setFontHeight((short) 24); + f4.setFontName("Testing 4"); + + HSSFCellStyle csU1 = wb.createCellStyle(); + csU1.setFont(f3); + HSSFCellStyle csU2 = wb.createCellStyle(); + csU2.setFont(f4); + + // Check before the optimise + assertEquals(8, wb.getNumberOfFonts()); + assertEquals(28, wb.getNumCellStyles()); + + // Optimise, should remove the two un-used ones and the one duplicate + HSSFOptimiser.optimiseCellStyles(wb); + + // Check + assertEquals(8, wb.getNumberOfFonts()); + assertEquals(25, wb.getNumCellStyles()); + + // csD -> cs1 -> 21 + assertEquals(21, r.getCell(8).getCellValueRecord().getXFIndex()); } } -- 2.39.5