]> source.dussan.org Git - poi.git/commitdiff
Fix bug #48494 - have EventBasedExcelExtractor make use of HSSFDataFormatter, so...
authorNick Burch <nick@apache.org>
Wed, 2 Jun 2010 16:13:04 +0000 (16:13 +0000)
committerNick Burch <nick@apache.org>
Wed, 2 Jun 2010 16:13:04 +0000 (16:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@950616 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java

index 65790b4f891fe27e77b2189fe23e06ad7d994184..016fb1eceb21f1d14104585442403beff33b8612 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-SNAPSHOT" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">48494 - have EventBasedExcelExtractor make use of HSSFDataFormatter, so that numbers and dates come out closer to how Excel would render them</action>
            <action dev="POI-DEVELOPERS" type="fix">49096 - add clone support to Chart begin and end records, to allow cloning of more Chart containing sheets</action>
            <action dev="POI-DEVELOPERS" type="add">List attachment names in the output of OutlookTextExtractor (to get attachment contents, use ExtractorFactory as normal)</action>
            <action dev="POI-DEVELOPERS" type="fix">48872 - allow DateFormatter.formatRawCellContents to handle 1904 as well as 1900 dates</action>
index 15560e19c99cafd6a9dde26e9329ef1d87d58879..8a7643b0132637cfb25d7c7fa0360f8d5145eec6 100644 (file)
 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);
-               }
        }
 }
index e0d6bfc99442035186c754a462b48a1a3f937423..bc264d097e3eebc9c2926064dac93c95dfd2fb46 100644 (file)
@@ -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
                );
        }