]> source.dussan.org Git - poi.git/commitdiff
Tweaked patch from bug #46287 - Control of header and footer extraction in ExcelExtra...
authorNick Burch <nick@apache.org>
Sun, 17 May 2009 18:49:29 +0000 (18:49 +0000)
committerNick Burch <nick@apache.org>
Sun, 17 May 2009 18:49:29 +0000 (18:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@775738 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java

index b47b164c8e7c3b13a24ca04384c3b7f5c4389318..158f096d09b5b9e423f1906d9fe829fe4e82fcbb 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <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">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>
index 8d595cbfebed2345944cf5493a1e2f5df257cb37..84f815edcbbb0f31afaaec8e68cf71a162b32fe5 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <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">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>
index e9a713099835f87ab785fa7b392860a54eee68c5..262f10a93665b1ceb4c9978d42ecde0b5edc7c8f 100644 (file)
@@ -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<nArgs; i++) {
                                String arg = args[i];
                                if ("-help".equalsIgnoreCase(arg)) {
@@ -127,6 +130,10 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
                                        showBlankCells = parseBoolArg(args, ++i);
                                        continue;
                                }
+                               if ("--headers-footers".equals(arg)) {
+                                       headersFooters = parseBoolArg(args, ++i);
+                                       continue;
+                               }
                                throw new CommandParseException("Invalid argument '" + arg + "'");
                        }
                        _requestHelp = requestHelp;
@@ -135,6 +142,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
                        _evaluateFormulas = evaluateFormulas;
                        _showCellComments = showCellComments;
                        _showBlankCells = showBlankCells;
+                       _headersFooters = headersFooters;
                }
                private static boolean parseBoolArg(String[] args, int i) throws CommandParseException {
                        if (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())
                                );
index 4d38f6f471a2036945f42c2e6c80f4beb77965be..8a17cf5d8b04bb3dbac9913579e7e9076ae20158 100644 (file)
@@ -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();