Fix indenting to be consistent, and correct the setIncludeCellComments javadoc to match the long standing default (#54871)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1496510 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-06-25 15:05:27 +00:00
parent dbd1a59416
commit ed5588d9af

View File

@ -40,64 +40,64 @@ import org.apache.xmlbeans.XmlException;
* Helper class to extract text from an OOXML Excel file * Helper class to extract text from an OOXML Excel file
*/ */
public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apache.poi.ss.extractor.ExcelExtractor { public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apache.poi.ss.extractor.ExcelExtractor {
public static final XSSFRelation[] SUPPORTED_TYPES = new XSSFRelation[] { public static final XSSFRelation[] SUPPORTED_TYPES = new XSSFRelation[] {
XSSFRelation.WORKBOOK, XSSFRelation.MACRO_TEMPLATE_WORKBOOK, XSSFRelation.WORKBOOK, XSSFRelation.MACRO_TEMPLATE_WORKBOOK,
XSSFRelation.MACRO_ADDIN_WORKBOOK, XSSFRelation.TEMPLATE_WORKBOOK, XSSFRelation.MACRO_ADDIN_WORKBOOK, XSSFRelation.TEMPLATE_WORKBOOK,
XSSFRelation.MACROS_WORKBOOK XSSFRelation.MACROS_WORKBOOK
}; };
private Locale locale; 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;
private boolean includeCellComments = false; private boolean includeCellComments = false;
private boolean includeHeadersFooters = true; private boolean includeHeadersFooters = true;
/** /**
* @deprecated Use {@link #XSSFExcelExtractor(org.apache.poi.openxml4j.opc.OPCPackage)} instead. * @deprecated Use {@link #XSSFExcelExtractor(org.apache.poi.openxml4j.opc.OPCPackage)} instead.
*/ */
public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException { public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException {
this(new XSSFWorkbook(path)); this(new XSSFWorkbook(path));
} }
public XSSFExcelExtractor(OPCPackage container) throws XmlException, OpenXML4JException, IOException { public XSSFExcelExtractor(OPCPackage container) throws XmlException, OpenXML4JException, IOException {
this(new XSSFWorkbook(container)); this(new XSSFWorkbook(container));
} }
public XSSFExcelExtractor(XSSFWorkbook workbook) { public XSSFExcelExtractor(XSSFWorkbook workbook) {
super(workbook); super(workbook);
this.workbook = workbook; this.workbook = workbook;
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if(args.length < 1) { if(args.length < 1) {
System.err.println("Use:"); System.err.println("Use:");
System.err.println(" XSSFExcelExtractor <filename.xlsx>"); System.err.println(" XSSFExcelExtractor <filename.xlsx>");
System.exit(1); System.exit(1);
} }
POIXMLTextExtractor extractor = POIXMLTextExtractor extractor =
new XSSFExcelExtractor(args[0]); new XSSFExcelExtractor(args[0]);
System.out.println(extractor.getText()); System.out.println(extractor.getText());
} }
/** /**
* Should sheet names be included? Default is true * Should sheet names be included? Default is true
*/ */
public void setIncludeSheetNames(boolean includeSheetNames) { public void setIncludeSheetNames(boolean includeSheetNames) {
this.includeSheetNames = includeSheetNames; this.includeSheetNames = includeSheetNames;
} }
/** /**
* Should we return the formula itself, and not * Should we return the formula itself, and not
* the result it produces? Default is false * the result it produces? Default is false
*/ */
public void setFormulasNotResults(boolean formulasNotResults) { public void setFormulasNotResults(boolean formulasNotResults) {
this.formulasNotResults = formulasNotResults; this.formulasNotResults = formulasNotResults;
} }
/** /**
* Should cell comments be included? Default is true * Should cell comments be included? Default is false
*/ */
public void setIncludeCellComments(boolean includeCellComments) { public void setIncludeCellComments(boolean includeCellComments) {
this.includeCellComments = includeCellComments; this.includeCellComments = includeCellComments;
} }
/** /**
* Should headers and footers be included? Default is true * Should headers and footers be included? Default is true
*/ */
public void setIncludeHeadersFooters(boolean includeHeadersFooters) { public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
@ -108,122 +108,122 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
* on the styles applied to the cells) * on the styles applied to the cells)
*/ */
public void setLocale(Locale locale) { public void setLocale(Locale locale) {
this.locale = locale; this.locale = locale;
} }
/**
* 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");
}
// Header(s), if present /**
if(includeHeadersFooters) { * Retreives the text contents of the file
text.append( */
extractHeaderFooter(sheet.getFirstHeader()) public String getText() {
); DataFormatter formatter;
text.append( if(locale == null) {
extractHeaderFooter(sheet.getOddHeader()) formatter = new DataFormatter();
); } else {
text.append( formatter = new DataFormatter(locale);
extractHeaderFooter(sheet.getEvenHeader()) }
);
}
// Rows and cells StringBuffer text = new StringBuffer();
for (Object rawR : sheet) { for(int i=0; i<workbook.getNumberOfSheets(); i++) {
Row row = (Row)rawR; XSSFSheet sheet = workbook.getSheetAt(i);
for(Iterator<Cell> ri = row.cellIterator(); ri.hasNext();) { if(includeSheetNames) {
Cell cell = ri.next(); text.append(workbook.getSheetName(i)).append("\n");
}
// Is it a formula one? // Header(s), if present
if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { if(includeHeadersFooters) {
if (formulasNotResults) { text.append(
text.append(cell.getCellFormula()); extractHeaderFooter(sheet.getFirstHeader())
} else { );
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { text.append(
handleStringCell(text, cell); extractHeaderFooter(sheet.getOddHeader())
} else { );
handleNonStringCell(text, cell, formatter); text.append(
} extractHeaderFooter(sheet.getEvenHeader())
} );
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { }
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
// Output the comment, if requested and exists // Rows and cells
Comment comment = cell.getCellComment(); for (Object rawR : sheet) {
if(includeCellComments && comment != null) { Row row = (Row)rawR;
// Replace any newlines with spaces, otherwise it for(Iterator<Cell> ri = row.cellIterator(); ri.hasNext();) {
// breaks the output Cell cell = ri.next();
String commentText = comment.getString().getString().replace('\n', ' ');
text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
}
if(ri.hasNext()) // Is it a formula one?
text.append("\t"); if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
} if (formulasNotResults) {
text.append("\n"); 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) {
handleStringCell(text, cell);
} else {
handleNonStringCell(text, cell, formatter);
}
// Finally footer(s), if present // Output the comment, if requested and exists
if(includeHeadersFooters) { Comment comment = cell.getCellComment();
text.append( if(includeCellComments && comment != null) {
extractHeaderFooter(sheet.getFirstFooter()) // Replace any newlines with spaces, otherwise it
); // breaks the output
text.append( String commentText = comment.getString().getString().replace('\n', ' ');
extractHeaderFooter(sheet.getOddFooter()) text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
); }
text.append(
extractHeaderFooter(sheet.getEvenFooter())
);
}
}
return text.toString(); if(ri.hasNext())
} text.append("\t");
}
private void handleStringCell(StringBuffer text, Cell cell) { text.append("\n");
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) { // Finally footer(s), if present
CellStyle cs = cell.getCellStyle(); if(includeHeadersFooters) {
text.append(
extractHeaderFooter(sheet.getFirstFooter())
);
text.append(
extractHeaderFooter(sheet.getOddFooter())
);
text.append(
extractHeaderFooter(sheet.getEvenFooter())
);
}
}
if (cs.getDataFormatString() != null) { return text.toString();
text.append(formatter.formatRawCellContents( }
cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()
));
return;
}
}
// No supported styling applies to this cell private void handleStringCell(StringBuffer text, Cell cell) {
XSSFCell xcell = (XSSFCell)cell; text.append(cell.getRichStringCellValue().getString());
text.append( xcell.getRawValue() ); }
} private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
int type = cell.getCellType();
if (type == Cell.CELL_TYPE_FORMULA) {
type = cell.getCachedFormulaResultType();
}
private String extractHeaderFooter(HeaderFooter hf) { if (type == Cell.CELL_TYPE_NUMERIC) {
return ExcelExtractor._extractHeaderFooter(hf); 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);
}
} }