From 487466212acbd61418dd895381690fea65bd1337 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 15 Jul 2008 21:38:38 +0000 Subject: [PATCH] Cell Style optimisations too git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@677057 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/model/Workbook.java | 12 +++ .../apache/poi/hssf/usermodel/HSSFCell.java | 3 +- .../poi/hssf/usermodel/HSSFOptimiser.java | 83 ++++++++++++++++ .../poi/hssf/usermodel/TestHSSFOptimiser.java | 95 ++++++++++++++++++- 4 files changed, 190 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index c9b4a0ee1a..36510eb835 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -713,6 +713,18 @@ public class Workbook implements Model return retval; } + + /** + * Removes the given ExtendedFormatRecord record from the + * file's list. This will make all + * subsequent font indicies drop by one, + * so you'll need to update those yourself! + */ + public void removeExFormatRecord(ExtendedFormatRecord rec) { + records.remove(rec); // this updates XfPos for us + numxfs--; + } + /** * creates a new Cell-type Extneded Format Record and adds it to the end of diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 636c5406fd..21335a8dd8 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -927,14 +927,13 @@ public class HSSFCell * object. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short) */ - public HSSFCellStyle getCellStyle() { short styleIndex=record.getXFIndex(); ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex); return new HSSFCellStyle(styleIndex, xf, book); } - + /** * used for internationalization, currently -1 for unchanged, 0 for compressed unicode or 1 for 16-bit * diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java index c01037ae49..f2c7acc414 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java @@ -173,6 +173,89 @@ public class HSSFOptimiser { * @param workbook The workbook in which to optimise the cell styles */ public static void optimiseCellStyles(HSSFWorkbook workbook) { + // Where each style has ended up, and if we need to + // delete the record for it. Start off with no change + short[] newPos = + new short[workbook.getWorkbook().getNumExFormats()]; + boolean[] zapRecords = new boolean[newPos.length]; + for(int i=0; i 21 + assertEquals(21, r.getCell(0).getCellValueRecord().getXFIndex()); + // cs2 -> 22 + assertEquals(22, r.getCell(1).getCellValueRecord().getXFIndex()); + // cs3 = cs1 -> 21 + assertEquals(21, r.getCell(2).getCellValueRecord().getXFIndex()); + // cs4 --> 24 -> 23 + assertEquals(23, r.getCell(3).getCellValueRecord().getXFIndex()); + // cs5 --> 25 -> 24 + assertEquals(24, r.getCell(4).getCellValueRecord().getXFIndex()); + // cs6 = cs2 -> 22 + assertEquals(22, r.getCell(5).getCellValueRecord().getXFIndex()); + // cs1 -> 21 + assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex()); + // cs2 -> 22 + assertEquals(22, r.getCell(7).getCellValueRecord().getXFIndex()); + } } -- 2.39.5