From 818091f54df3dc0dae7873bc19dd46bfa1defc2f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 17 Oct 2023 15:22:16 +0000 Subject: [PATCH] [bug-67784] experimental hack to fix regression git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913067 13f79535-47bb-0310-9956-ffa450edef68 --- .../eventusermodel/XSSFSheetXMLHandler.java | 125 +++++++++--------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java index 42dca23e30..ec6d1a0872 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java @@ -215,7 +215,9 @@ public class XSSFSheetXMLHandler extends DefaultHandler { formula.setLength(0); // Mark us as being a formula if not already - nextDataType = xssfDataType.FORMULA; + if (this.nextDataType == XSSFSheetXMLHandler.xssfDataType.NUMBER) { + this.nextDataType = XSSFSheetXMLHandler.xssfDataType.FORMULA; + } // Decide where to get the formula string from String type = attributes.getValue("t"); @@ -260,6 +262,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { // c => cell else if ("c".equals(localName)) { // Set up defaults. + this.formula.setLength(0); this.nextDataType = xssfDataType.NUMBER; this.formatIndex = -1; this.formatString = null; @@ -367,68 +370,72 @@ public class XSSFSheetXMLHandler extends DefaultHandler { String thisStr = null; // Process the value contents as required, now we have it all - switch (nextDataType) { - case BOOLEAN: - char first = value.charAt(0); - thisStr = first == '0' ? "FALSE" : "TRUE"; - break; - - case ERROR: - thisStr = "ERROR:" + value; - break; - - case FORMULA: - if (formulasNotResults) { - thisStr = formula.toString(); - } else { - String fv = value.toString(); - - if (this.formatString != null) { - try { - // Try to use the value as a formattable number - double d = Double.parseDouble(fv); - thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString); - } catch (NumberFormatException e) { - // Formula is a String result not a Numeric one + if (formulasNotResults && formula.length() > 0) { + thisStr = formula.toString(); + } else { + switch (nextDataType) { + case BOOLEAN: + char first = value.charAt(0); + thisStr = first == '0' ? "FALSE" : "TRUE"; + break; + + case ERROR: + thisStr = "ERROR:" + value; + break; + + case FORMULA: + if (formulasNotResults) { + thisStr = formula.toString(); + } else { + String fv = value.toString(); + + if (this.formatString != null) { + try { + // Try to use the value as a formattable number + double d = Double.parseDouble(fv); + thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString); + } catch (NumberFormatException e) { + // Formula is a String result not a Numeric one + thisStr = fv; + } + } else { + // No formatting applied, just do raw value in all cases thisStr = fv; } - } else { - // No formatting applied, just do raw value in all cases - thisStr = fv; } - } - break; - - case INLINE_STRING: - // TODO: Can these ever have formatting on them? - XSSFRichTextString rtsi = new XSSFRichTextString(value.toString()); - thisStr = rtsi.toString(); - break; - - case SST_STRING: - String sstIndex = value.toString(); - if (sstIndex.length() > 0) { - try { - int idx = Integer.parseInt(sstIndex); - RichTextString rtss = sharedStringsTable.getItemAt(idx); - thisStr = rtss.toString(); - } catch (NumberFormatException ex) { - LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex); + break; + + case INLINE_STRING: + // TODO: Can these ever have formatting on them? + XSSFRichTextString rtsi = new XSSFRichTextString(value.toString()); + thisStr = rtsi.toString(); + break; + + case SST_STRING: + String sstIndex = value.toString(); + if (sstIndex.length() > 0) { + try { + int idx = Integer.parseInt(sstIndex); + RichTextString rtss = sharedStringsTable.getItemAt(idx); + thisStr = rtss.toString(); + } catch (NumberFormatException ex) { + LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex); + } } - } - break; - - case NUMBER: - String n = value.toString(); - if (this.formatString != null && n.length() > 0) - thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString); - else - thisStr = n; - break; - - default: - thisStr = "(TODO: Unexpected type: " + nextDataType + ")"; - break; + break; + + case NUMBER: + String n = value.toString(); + if (this.formatString != null && n.length() > 0) + thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString); + else + thisStr = n; + break; + + default: + thisStr = "(TODO: Unexpected type: " + nextDataType + ")"; + break; + } } // Do we have a comment for this cell? -- 2.39.5