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
This commit is contained in:
parent
b288c631f2
commit
393e672176
@ -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>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user