]> source.dussan.org Git - poi.git/commitdiff
[bug-67785] make XSSFExcelExtractor output more like that from XSSFEventBasedExcelExt...
authorPJ Fanning <fanningpj@apache.org>
Wed, 18 Oct 2023 09:37:37 +0000 (09:37 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 18 Oct 2023 09:37:37 +0000 (09:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913080 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
poi-ooxml/src/test/java/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java

index 5524ded59bd3e79e9ab2af4ae5a598237c7fd4fd..da03c669d509a7b5d5f12b29d8513bd69383225d 100644 (file)
@@ -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);
         }
index 40fc0602b7a3c2feebc629672969d87d2a892ea2..f6d497082dfd34bfba9e9aeac00deb47d1d423db 100644 (file)
@@ -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")) {