From 9a8ea4a165bce960bedc499d73e6eba5457031c7 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 18 Oct 2023 09:37:37 +0000 Subject: [PATCH] [bug-67785] make XSSFExcelExtractor output more like that from XSSFEventBasedExcelExtractor git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913080 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/extractor/XSSFExcelExtractor.java | 9 ++++++++- .../poi/xssf/extractor/TestXSSFExcelExtractor.java | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 5524ded59b..da03c669d5 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -55,6 +55,7 @@ public class XSSFExcelExtractor private Locale locale; private final XSSFWorkbook workbook; + private final DataFormatter dataFormatter; private boolean includeSheetNames = true; private boolean formulasNotResults; private boolean includeCellComments; @@ -67,6 +68,8 @@ public class XSSFExcelExtractor } public XSSFExcelExtractor(XSSFWorkbook workbook) { this.workbook = workbook; + this.dataFormatter = new DataFormatter(); + this.dataFormatter.setUseCachedValuesForFormulaCells(true); } /** @@ -243,8 +246,12 @@ public class XSSFExcelExtractor } // No supported styling applies to this cell - String contents = ((XSSFCell)cell).getRawValue(); + String contents = dataFormatter.formatCellValue(cell); if (contents != null) { + if (type == CellType.ERROR) { + // to match what XSSFEventBasedExcelExtractor does + contents = "ERROR:" + contents; + } checkMaxTextSize(text, contents); text.append(contents); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java index 40fc0602b7..f6d497082d 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java @@ -247,6 +247,17 @@ class TestXSSFExcelExtractor { } } + @Test + void test67784() throws Exception { + try (XSSFExcelExtractor extractor = getExtractor("bug67784.xlsx")) { + String text = extractor.getText().replace("\r", ""); + String[] lines = text.split("\n"); + assertEquals("FALSE", lines[2]); + assertEquals("TRUE", lines[3]); + assertEquals("ERROR:#DIV/0!", lines[4]); + } + } + @Test void test67784Formulas() throws Exception { try (XSSFExcelExtractor extractor = getExtractor("bug67784.xlsx")) { -- 2.39.5