You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2010/06/02 18:13:05 UTC

svn commit: r950616 - in /poi/trunk/src: documentation/content/xdocs/status.xml java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java

Author: nick
Date: Wed Jun  2 16:13:04 2010
New Revision: 950616

URL: http://svn.apache.org/viewvc?rev=950616&view=rev
Log:
Fix bug #48494 - have EventBasedExcelExtractor make use of HSSFDataFormatter, so that numbers and dates come out closer to how Excel would render them

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

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=950616&r1=950615&r2=950616&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Jun  2 16:13:04 2010
@@ -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>

Modified: poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java?rev=950616&r1=950615&r2=950616&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java Wed Jun  2 16:13:04 2010
@@ -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.eventusermode
 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.Number
 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 ex
 						outputNextStringValue = true;
 						nextRow = frec.getRow();
 					} else {
-						thisText = formatNumberDateCell(frec, frec.getValue());
+						thisText = _ft.formatNumberDateCell(frec); 
 					}
 				}
 				break;
@@ -240,7 +234,7 @@ public class EventBasedExcelExtractor ex
 			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 ex
 				_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);
-		}
 	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java?rev=950616&r1=950615&r2=950616&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java Wed Jun  2 16:13:04 2010
@@ -158,11 +158,11 @@ public final class TestExcelExtractor ex
 
 		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
 		);
 	}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org