From 0d1692dbf270fe2aaa2b9c296385b3293d8739b9 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 2 Jun 2010 16:13:04 +0000 Subject: [PATCH] Fix bug #48494 - have EventBasedExcelExtractor make use of HSSFDataFormatter, so that numbers and dates come out closer to how Excel would render them git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@950616 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../extractor/EventBasedExcelExtractor.java | 45 +------------------ .../hssf/extractor/TestExcelExtractor.java | 10 ++--- 3 files changed, 8 insertions(+), 48 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 65790b4f89..016fb1eceb 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 48494 - have EventBasedExcelExtractor make use of HSSFDataFormatter, so that numbers and dates come out closer to how Excel would render them 49096 - add clone support to Chart begin and end records, to allow cloning of more Chart containing sheets List attachment names in the output of OutlookTextExtractor (to get attachment contents, use ExtractorFactory as normal) 48872 - allow DateFormatter.formatRawCellContents to handle 1904 as well as 1900 dates diff --git a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java index 15560e19c9..8a7643b013 100644 --- a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java @@ -18,11 +18,7 @@ package org.apache.poi.hssf.extractor; import java.io.IOException; -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; import org.apache.poi.POIOLE2TextExtractor; @@ -35,7 +31,6 @@ import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BoundSheetRecord; -import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.LabelRecord; import org.apache.poi.hssf.record.LabelSSTRecord; @@ -44,7 +39,6 @@ import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SSTRecord; import org.apache.poi.hssf.record.StringRecord; -import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -206,7 +200,7 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor { outputNextStringValue = true; nextRow = frec.getRow(); } else { - thisText = formatNumberDateCell(frec, frec.getValue()); + thisText = _ft.formatNumberDateCell(frec); } } break; @@ -240,7 +234,7 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor { case NumberRecord.sid: NumberRecord numrec = (NumberRecord) record; thisRow = numrec.getRow(); - thisText = formatNumberDateCell(numrec, numrec.getValue()); + thisText = _ft.formatNumberDateCell(numrec); break; default: break; @@ -257,40 +251,5 @@ public class EventBasedExcelExtractor extends POIOLE2TextExtractor { _text.append(thisText); } } - - /** - * Formats a number or date cell, be that a real number, or the - * answer to a formula - */ - private String formatNumberDateCell(CellValueRecordInterface cell, double value) { - // Get the built in format, if there is one - int formatIndex = _ft.getFormatIndex(cell); - String formatString = _ft.getFormatString(cell); - - if(formatString == null) { - return Double.toString(value); - } - // Is it a date? - if(HSSFDateUtil.isADateFormat(formatIndex,formatString) && - HSSFDateUtil.isValidExcelDate(value)) { - // Java wants M not m for month - formatString = formatString.replace('m','M'); - // Change \- into -, if it's there - formatString = formatString.replaceAll("\\\\-","-"); - - // Format as a date - Date d = HSSFDateUtil.getJavaDate(value, false); - DateFormat df = new SimpleDateFormat(formatString); - return df.format(d); - } - if(formatString == "General") { - // Some sort of wierd default - return Double.toString(value); - } - - // Format as a number - DecimalFormat df = new DecimalFormat(formatString); - return df.format(value); - } } } diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java index e0d6bfc994..bc264d097e 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java @@ -158,11 +158,11 @@ public final class TestExcelExtractor extends TestCase { text = extractor.getText(); assertEquals( - "1000.0\t1.0\tSUMIF(A1:A5,\">4000\",B1:B5)\n" + - "2000.0\t2.0\n" + - "3000.0\t3.0\n" + - "4000.0\t4.0\n" + - "5000.0\t5.0\n", + "1000\t1\tSUMIF(A1:A5,\">4000\",B1:B5)\n" + + "2000\t2\n" + + "3000\t3\n" + + "4000\t4\n" + + "5000\t5\n", text ); } -- 2.39.5