Tweaked patch from bug #46287 - Control of header and footer extraction in ExcelExtractor / XSSFExcelExtractor

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@775738 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2009-05-17 18:49:29 +00:00
parent a3c5fba15c
commit 8cfca324f1
4 changed files with 53 additions and 21 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.5-beta6" date="2009-??-??"> <release version="3.5-beta6" date="2009-??-??">
<action dev="POI-DEVELOPERS" type="add">46287 - Control of header and footer extraction in ExcelExtractor / XSSFExcelExtractor</action>
<action dev="POI-DEVELOPERS" type="add">46554 - New ant target "jar-examples"</action> <action dev="POI-DEVELOPERS" type="add">46554 - New ant target "jar-examples"</action>
<action dev="POI-DEVELOPERS" type="add">46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed</action> <action dev="POI-DEVELOPERS" type="add">46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed</action>
<action dev="POI-DEVELOPERS" type="add">46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas</action> <action dev="POI-DEVELOPERS" type="add">46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.5-beta6" date="2009-??-??"> <release version="3.5-beta6" date="2009-??-??">
<action dev="POI-DEVELOPERS" type="add">46287 - Control of header and footer extraction in ExcelExtractor / XSSFExcelExtractor</action>
<action dev="POI-DEVELOPERS" type="add">46554 - New ant target "jar-examples"</action> <action dev="POI-DEVELOPERS" type="add">46554 - New ant target "jar-examples"</action>
<action dev="POI-DEVELOPERS" type="add">46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed</action> <action dev="POI-DEVELOPERS" type="add">46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed</action>
<action dev="POI-DEVELOPERS" type="add">46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas</action> <action dev="POI-DEVELOPERS" type="add">46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas</action>

View File

@ -55,6 +55,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
private boolean _shouldEvaluateFormulas = true; private boolean _shouldEvaluateFormulas = true;
private boolean _includeCellComments = false; private boolean _includeCellComments = false;
private boolean _includeBlankCells = false; private boolean _includeBlankCells = false;
private boolean _includeHeadersFooters = true;
public ExcelExtractor(HSSFWorkbook wb) { public ExcelExtractor(HSSFWorkbook wb) {
super(wb); super(wb);
@ -79,6 +80,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
private final boolean _evaluateFormulas; private final boolean _evaluateFormulas;
private final boolean _showCellComments; private final boolean _showCellComments;
private final boolean _showBlankCells; private final boolean _showBlankCells;
private final boolean _headersFooters;
public CommandArgs(String[] args) throws CommandParseException { public CommandArgs(String[] args) throws CommandParseException {
int nArgs = args.length; int nArgs = args.length;
File inputFile = null; File inputFile = null;
@ -87,6 +89,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
boolean evaluateFormulas = true; boolean evaluateFormulas = true;
boolean showCellComments = false; boolean showCellComments = false;
boolean showBlankCells = false; boolean showBlankCells = false;
boolean headersFooters = true;
for (int i=0; i<nArgs; i++) { for (int i=0; i<nArgs; i++) {
String arg = args[i]; String arg = args[i];
if ("-help".equalsIgnoreCase(arg)) { if ("-help".equalsIgnoreCase(arg)) {
@ -127,6 +130,10 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
showBlankCells = parseBoolArg(args, ++i); showBlankCells = parseBoolArg(args, ++i);
continue; continue;
} }
if ("--headers-footers".equals(arg)) {
headersFooters = parseBoolArg(args, ++i);
continue;
}
throw new CommandParseException("Invalid argument '" + arg + "'"); throw new CommandParseException("Invalid argument '" + arg + "'");
} }
_requestHelp = requestHelp; _requestHelp = requestHelp;
@ -135,6 +142,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
_evaluateFormulas = evaluateFormulas; _evaluateFormulas = evaluateFormulas;
_showCellComments = showCellComments; _showCellComments = showCellComments;
_showBlankCells = showBlankCells; _showBlankCells = showBlankCells;
_headersFooters = headersFooters;
} }
private static boolean parseBoolArg(String[] args, int i) throws CommandParseException { private static boolean parseBoolArg(String[] args, int i) throws CommandParseException {
if (i >= args.length) { if (i >= args.length) {
@ -167,7 +175,9 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
public boolean shouldShowBlankCells() { public boolean shouldShowBlankCells() {
return _showBlankCells; return _showBlankCells;
} }
public boolean shouldIncludeHeadersFooters() {
return _headersFooters;
}
} }
private static void printUsageMessage(PrintStream ps) { private static void printUsageMessage(PrintStream ps) {
@ -180,6 +190,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
ps.println(" --evaluate-formulas Y"); ps.println(" --evaluate-formulas Y");
ps.println(" --show-comments N"); ps.println(" --show-comments N");
ps.println(" --show-blanks Y"); ps.println(" --show-blanks Y");
ps.println(" --headers-footers Y");
} }
/** /**
@ -216,6 +227,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas()); extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas());
extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments()); extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments());
extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells()); extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells());
extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters());
System.out.println(extractor.getText()); System.out.println(extractor.getText());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -249,6 +261,13 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
public void setIncludeBlankCells(boolean includeBlankCells) { public void setIncludeBlankCells(boolean includeBlankCells) {
_includeBlankCells = includeBlankCells; _includeBlankCells = includeBlankCells;
} }
/**
* Should headers and footers be included in the output?
* Default is to include them.
*/
public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
_includeHeadersFooters = includeHeadersFooters;
}
/** /**
* Retrieves the text contents of the file * Retrieves the text contents of the file
@ -274,7 +293,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
} }
// Header text, if there is any // Header text, if there is any
if(sheet.getHeader() != null) { if(_includeHeadersFooters && sheet.getHeader() != null) {
text.append( text.append(
_extractHeaderFooter(sheet.getHeader()) _extractHeaderFooter(sheet.getHeader())
); );
@ -364,7 +383,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
} }
// Finally Feader text, if there is any // Finally Feader text, if there is any
if(sheet.getFooter() != null) { if(_includeHeadersFooters && sheet.getFooter() != null) {
text.append( text.append(
_extractHeaderFooter(sheet.getFooter()) _extractHeaderFooter(sheet.getFooter())
); );

View File

@ -40,6 +40,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
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;
public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException { public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException {
this(new XSSFWorkbook(path)); this(new XSSFWorkbook(path));
@ -82,6 +83,12 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
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
*/
public void setIncludeHeadersFooters(boolean includeHeadersFooters) {
this.includeHeadersFooters = includeHeadersFooters;
}
/** /**
* Retreives the text contents of the file * Retreives the text contents of the file
@ -96,6 +103,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
} }
// Header(s), if present // Header(s), if present
if(includeHeadersFooters) {
text.append( text.append(
extractHeaderFooter(sheet.getFirstHeader()) extractHeaderFooter(sheet.getFirstHeader())
); );
@ -105,6 +113,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
text.append( text.append(
extractHeaderFooter(sheet.getEvenHeader()) extractHeaderFooter(sheet.getEvenHeader())
); );
}
// Rows and cells // Rows and cells
for (Object rawR : sheet) { for (Object rawR : sheet) {
@ -138,6 +147,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
} }
// Finally footer(s), if present // Finally footer(s), if present
if(includeHeadersFooters) {
text.append( text.append(
extractHeaderFooter(sheet.getFirstFooter()) extractHeaderFooter(sheet.getFirstFooter())
); );
@ -148,6 +158,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
extractHeaderFooter(sheet.getEvenFooter()) extractHeaderFooter(sheet.getEvenFooter())
); );
} }
}
return text.toString(); return text.toString();
} }