Bug #52369 - XSSFExcelExtractor should format numeric cells based on the format strings applied to them

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1221108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-12-20 05:54:18 +00:00
parent 9250a2e019
commit 6b0bce44a5
3 changed files with 64 additions and 13 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="fix">52369 - XSSFExcelExtractor should format numeric cells based on the format strings applied to them</action>
<action dev="poi-developers" type="fix">52369 - Event based XSSF parsing should handle formatting of formula values in XSSFSheetXMLHandler</action>
<action dev="poi-developers" type="fix">52348 - Avoid exception when creating cell style in a workbook that has an empty xf table</action>
<action dev="poi-developers" type="fix">52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString</action>

View File

@ -254,7 +254,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
thisStr = fv;
}
} else {
// No formatter supplied, just do raw value in all cases
// No formating applied, just do raw value in all cases
thisStr = fv;
}
}

View File

@ -18,13 +18,16 @@ package org.apache.poi.xssf.extractor;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
@ -43,6 +46,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
XSSFRelation.MACROS_WORKBOOK
};
private Locale locale;
private XSSFWorkbook workbook;
private boolean includeSheetNames = true;
private boolean formulasNotResults = false;
@ -96,14 +100,28 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
this.includeHeadersFooters = includeHeadersFooters;
}
/**
* What Locale should be used for formatting numbers (based
* on the styles applied to the cells)
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
/**
* Retreives the text contents of the file
*/
public String getText() {
StringBuffer text = new StringBuffer();
for(int i=0; i<workbook.getNumberOfSheets(); i++) {
/**
* Retreives the text contents of the file
*/
public String getText() {
DataFormatter formatter;
if(locale == null) {
formatter = new DataFormatter();
} else {
formatter = new DataFormatter(locale);
}
StringBuffer text = new StringBuffer();
for(int i=0; i<workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
if(includeSheetNames) {
text.append(workbook.getSheetName(i)).append("\n");
@ -129,13 +147,20 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
Cell cell = ri.next();
// Is it a formula one?
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulasNotResults) {
text.append(cell.getCellFormula());
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
if (formulasNotResults) {
text.append(cell.getCellFormula());
} else {
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
}
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
text.append(cell.getRichStringCellValue().getString());
handleStringCell(text, cell);
} else {
XSSFCell xc = (XSSFCell)cell;
text.append(xc.getRawValue());
handleNonStringCell(text, cell, formatter);
}
// Output the comment, if requested and exists
@ -169,6 +194,31 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
return text.toString();
}
private void handleStringCell(StringBuffer text, Cell cell) {
text.append(cell.getRichStringCellValue().getString());
}
private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
int type = cell.getCellType();
if (type == Cell.CELL_TYPE_FORMULA) {
type = cell.getCachedFormulaResultType();
}
if (type == Cell.CELL_TYPE_NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs.getDataFormatString() != null) {
text.append(formatter.formatRawCellContents(
cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()
));
return;
}
}
// No supported styling applies to this cell
XSSFCell xcell = (XSSFCell)cell;
text.append( xcell.getRawValue() );
}
private String extractHeaderFooter(HeaderFooter hf) {
return ExcelExtractor._extractHeaderFooter(hf);