mirror of https://github.com/apache/poi.git
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:
parent
7371290ff7
commit
c156c039c4
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.8-beta6" date="2012-??-??">
|
<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">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">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>
|
<action dev="poi-developers" type="fix">52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString</action>
|
||||||
|
|
|
@ -254,7 +254,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
|
||||||
thisStr = fv;
|
thisStr = fv;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// No formatter supplied, just do raw value in all cases
|
// No formating applied, just do raw value in all cases
|
||||||
thisStr = fv;
|
thisStr = fv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,13 +18,16 @@ package org.apache.poi.xssf.extractor;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.apache.poi.POIXMLTextExtractor;
|
import org.apache.poi.POIXMLTextExtractor;
|
||||||
import org.apache.poi.hssf.extractor.ExcelExtractor;
|
import org.apache.poi.hssf.extractor.ExcelExtractor;
|
||||||
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
||||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
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.Comment;
|
||||||
|
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||||
import org.apache.poi.ss.usermodel.HeaderFooter;
|
import org.apache.poi.ss.usermodel.HeaderFooter;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||||
|
@ -43,6 +46,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
|
||||||
XSSFRelation.MACROS_WORKBOOK
|
XSSFRelation.MACROS_WORKBOOK
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private Locale locale;
|
||||||
private XSSFWorkbook workbook;
|
private XSSFWorkbook workbook;
|
||||||
private boolean includeSheetNames = true;
|
private boolean includeSheetNames = true;
|
||||||
private boolean formulasNotResults = false;
|
private boolean formulasNotResults = false;
|
||||||
|
@ -96,14 +100,28 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
|
||||||
public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
|
public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
|
||||||
this.includeHeadersFooters = 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
|
* Retreives the text contents of the file
|
||||||
*/
|
*/
|
||||||
public String getText() {
|
public String getText() {
|
||||||
StringBuffer text = new StringBuffer();
|
DataFormatter formatter;
|
||||||
|
if(locale == null) {
|
||||||
for(int i=0; i<workbook.getNumberOfSheets(); i++) {
|
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);
|
XSSFSheet sheet = workbook.getSheetAt(i);
|
||||||
if(includeSheetNames) {
|
if(includeSheetNames) {
|
||||||
text.append(workbook.getSheetName(i)).append("\n");
|
text.append(workbook.getSheetName(i)).append("\n");
|
||||||
|
@ -129,13 +147,20 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
|
||||||
Cell cell = ri.next();
|
Cell cell = ri.next();
|
||||||
|
|
||||||
// Is it a formula one?
|
// Is it a formula one?
|
||||||
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulasNotResults) {
|
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
|
||||||
text.append(cell.getCellFormula());
|
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) {
|
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
|
||||||
text.append(cell.getRichStringCellValue().getString());
|
handleStringCell(text, cell);
|
||||||
} else {
|
} else {
|
||||||
XSSFCell xc = (XSSFCell)cell;
|
handleNonStringCell(text, cell, formatter);
|
||||||
text.append(xc.getRawValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output the comment, if requested and exists
|
// Output the comment, if requested and exists
|
||||||
|
@ -169,6 +194,31 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
|
||||||
|
|
||||||
return text.toString();
|
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) {
|
private String extractHeaderFooter(HeaderFooter hf) {
|
||||||
return ExcelExtractor._extractHeaderFooter(hf);
|
return ExcelExtractor._extractHeaderFooter(hf);
|
||||||
|
|
Loading…
Reference in New Issue