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! -->
<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>

View 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>

View 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())
);

View 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();