diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index b47b164c8..158f096d0 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 46287 - Control of header and footer extraction in ExcelExtractor / XSSFExcelExtractor 46554 - New ant target "jar-examples" 46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed 46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8d595cbfe..84f815edc 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 46287 - Control of header and footer extraction in ExcelExtractor / XSSFExcelExtractor 46554 - New ant target "jar-examples" 46161 - Support in XSSF for setGroupColumnCollapsed and setGroupRowCollapsed 46806 - Allow columns greater than 255 and rows greater than 0x100000 in XSSF formulas diff --git a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index e9a713099..262f10a93 100644 --- a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -55,6 +55,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p private boolean _shouldEvaluateFormulas = true; private boolean _includeCellComments = false; private boolean _includeBlankCells = false; + private boolean _includeHeadersFooters = true; public ExcelExtractor(HSSFWorkbook wb) { super(wb); @@ -79,6 +80,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p private final boolean _evaluateFormulas; private final boolean _showCellComments; private final boolean _showBlankCells; + private final boolean _headersFooters; public CommandArgs(String[] args) throws CommandParseException { int nArgs = args.length; File inputFile = null; @@ -87,6 +89,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p boolean evaluateFormulas = true; boolean showCellComments = false; boolean showBlankCells = false; + boolean headersFooters = true; for (int i=0; i= args.length) { @@ -167,7 +175,9 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p public boolean shouldShowBlankCells() { return _showBlankCells; } - + public boolean shouldIncludeHeadersFooters() { + return _headersFooters; + } } 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(" --show-comments N"); 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.setIncludeCellComments(cmdArgs.shouldShowCellComments()); extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells()); + extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters()); System.out.println(extractor.getText()); } catch (Exception e) { e.printStackTrace(); @@ -249,6 +261,13 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p public void setIncludeBlankCells(boolean 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 @@ -274,7 +293,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p } // Header text, if there is any - if(sheet.getHeader() != null) { + if(_includeHeadersFooters && sheet.getHeader() != null) { text.append( _extractHeaderFooter(sheet.getHeader()) ); @@ -364,7 +383,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p } // Finally Feader text, if there is any - if(sheet.getFooter() != null) { + if(_includeHeadersFooters && sheet.getFooter() != null) { text.append( _extractHeaderFooter(sheet.getFooter()) ); diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 4d38f6f47..8a17cf5d8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -40,6 +40,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach private boolean includeSheetNames = true; private boolean formulasNotResults = false; private boolean includeCellComments = false; + private boolean includeHeadersFooters = true; public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException { this(new XSSFWorkbook(path)); @@ -82,6 +83,12 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach public void setIncludeCellComments(boolean 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 @@ -96,15 +103,17 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach } // Header(s), if present - text.append( - extractHeaderFooter(sheet.getFirstHeader()) - ); - text.append( - extractHeaderFooter(sheet.getOddHeader()) - ); - text.append( - extractHeaderFooter(sheet.getEvenHeader()) - ); + if(includeHeadersFooters) { + text.append( + extractHeaderFooter(sheet.getFirstHeader()) + ); + text.append( + extractHeaderFooter(sheet.getOddHeader()) + ); + text.append( + extractHeaderFooter(sheet.getEvenHeader()) + ); + } // Rows and cells for (Object rawR : sheet) { @@ -138,15 +147,17 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach } // Finally footer(s), if present - text.append( - extractHeaderFooter(sheet.getFirstFooter()) - ); - text.append( - extractHeaderFooter(sheet.getOddFooter()) - ); - text.append( - extractHeaderFooter(sheet.getEvenFooter()) - ); + if(includeHeadersFooters) { + text.append( + extractHeaderFooter(sheet.getFirstFooter()) + ); + text.append( + extractHeaderFooter(sheet.getOddFooter()) + ); + text.append( + extractHeaderFooter(sheet.getEvenFooter()) + ); + } } return text.toString();