diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index c9b4a0ee1..36510eb83 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 636c5406f..21335a8dd 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 c01037ae4..f2c7acc41 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()); + } }