From b01c8e342ca4e3a04a99f1f841bcf9ec0084ef75 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Tue, 29 Dec 2015 00:12:04 +0000 Subject: [PATCH] =?utf8?q?bug=2058778:=20override=20a=20built-in=20number?= =?utf8?q?=20format,=20such=20as=20using=20=C2=A3=20instead=20of=20$=20for?= =?utf8?q?=20currency?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1722043 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/model/StylesTable.java | 16 +++++++++- .../poi/xssf/usermodel/XSSFDataFormat.java | 13 ++++++++ .../xssf/usermodel/TestXSSFDataFormat.java | 31 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 30e9f79ec4..66605e6a11 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -234,7 +234,7 @@ public class StylesTable extends POIXMLDocumentPart { throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!"); } - if (numberFormats.size() > MAXIMUM_NUMBER_OF_DATA_FORMATS) { + if (numberFormats.size() >= MAXIMUM_NUMBER_OF_DATA_FORMATS) { throw new IllegalStateException("The maximum number of Data Formats was exceeded. " + "You can define up to " + MAXIMUM_NUMBER_OF_DATA_FORMATS + " formats in a .xlsx Workbook."); } @@ -256,6 +256,20 @@ public class StylesTable extends POIXMLDocumentPart { numberFormats.put(formatIndex, fmt); return formatIndex; } + + + /** + * Add a number format with a specific ID into the numberFormats map. + * If a format with the same ID already exists, overwrite the format code + * with fmt + * This may be used to override built-in number formats. + * + * @param index the number format ID + * @param fmt the number format code + */ + public void putNumberFormat(short index, String fmt) { + numberFormats.put((int)index, fmt); + } public XSSFFont getFontAt(int idx) { return fonts.get(idx); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java index b17e1bb17f..4b117742ea 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java @@ -78,4 +78,17 @@ public class XSSFDataFormat implements DataFormat { if(fmt == null) fmt = BuiltinFormats.getBuiltinFormat(index); return fmt; } + + /** + * Add a number format with a specific ID into the number format style table. + * If a format with the same ID already exists, overwrite the format code + * with fmt + * This may be used to override built-in number formats. + * + * @param index the number format ID + * @param format the number format code + */ + public void putFormat(short index, String format) { + stylesSource.putNumberFormat(index, format); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java index d2a9e5ba2c..fd19c4b1e9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java @@ -17,8 +17,12 @@ package org.apache.poi.xssf.usermodel; +import java.io.IOException; + import org.apache.poi.ss.usermodel.BaseTestDataFormat; import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -62,4 +66,31 @@ public final class TestXSSFDataFormat extends BaseTestDataFormat { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("FormatKM.xlsx"); doTest58532Core(wb); } + + public void test58778() throws IOException { + XSSFWorkbook wb = new XSSFWorkbook(); + Cell cell = wb.createSheet("bug58778").createRow(0).createCell(0); + cell.setCellValue(5.25); + CellStyle style = wb.createCellStyle(); + + XSSFDataFormat dataFormat = wb.createDataFormat(); + + short poundFmtIdx = 6; + dataFormat.putFormat(poundFmtIdx, poundFmt); + style.setDataFormat(poundFmtIdx); + cell.setCellStyle(style); + // Cell should appear as "5" + + wb = XSSFTestDataSamples.writeOutCloseAndReadBack(wb); + cell = wb.getSheet("bug58778").getRow(0).getCell(0); + assertEquals(5.25, cell.getNumericCellValue()); + + style = cell.getCellStyle(); + assertEquals(poundFmt, style.getDataFormatString()); + assertEquals(poundFmtIdx, style.getDataFormat()); + + // manually check the file to make sure the cell is rendered as "5" + // Verified with LibreOffice 4.2.8.2 on 2015-12-28 + wb.close(); + } } -- 2.39.5