Bring over some fixes from XSSFSheetXMLHandler, and note that in the javadocs

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695309 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-08-11 14:31:14 +00:00
parent e2244c83f8
commit 7a21e70281

View File

@ -31,6 +31,8 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRichTextString;
@ -57,12 +59,12 @@ import org.xml.sax.helpers.DefaultHandler;
* because the standard POI SharedStringsTable grows very * because the standard POI SharedStringsTable grows very
* quickly with the number of unique strings. * quickly with the number of unique strings.
* <p/> * <p/>
* Thanks to Eric Smith for a patch that fixes a problem * For a more advanced implementation of SAX event parsing
* triggered by cells with multiple "t" elements, which is * of XLSX files, see {@link XSSFEventBasedExcelExtractor}
* how Excel represents different formats (e.g., one word * and {@link XSSFSheetXMLHandler}. Note that some use cases,
* plain and one word bold). * it may be possible to simply use those with a custom
* * {@link SheetContentsHandler} and no SAX code needed of
* @author Chris Lott * your own!
*/ */
public class XLSX2CSV { public class XLSX2CSV {
@ -195,12 +197,19 @@ public class XLSX2CSV {
else if (cellStyleStr != null) { else if (cellStyleStr != null) {
// It's a number, but almost certainly one // It's a number, but almost certainly one
// with a special style or format // with a special style or format
int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = null;
XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); if (cellStyleStr != null) {
this.formatIndex = style.getDataFormat(); int styleIndex = Integer.parseInt(cellStyleStr);
this.formatString = style.getDataFormatString(); style = stylesTable.getStyleAt(styleIndex);
if (this.formatString == null) } else if (stylesTable.getNumCellStyles() > 0) {
this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex); style = stylesTable.getStyleAt(0);
}
if (style != null) {
this.formatIndex = style.getDataFormat();
this.formatString = style.getDataFormatString();
if (this.formatString == null)
this.formatString = BuiltinFormats.getBuiltinFormat(this.formatIndex);
}
} }
} }
@ -256,7 +265,7 @@ public class XLSX2CSV {
case NUMBER: case NUMBER:
String n = value.toString(); String n = value.toString();
if (this.formatString != null) if (this.formatString != null && n.length() > 0)
thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString); thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
else else
thisStr = n; thisStr = n;