Fix bug #45538 - Include excel headers and footers in the output of ExcelExtractor

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@682511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-08-04 21:21:16 +00:00
parent 7cd7a7b275
commit 10d561380d
11 changed files with 92 additions and 2 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action>
<action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action>
<action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action>
<action dev="POI-DEVELOPERS" type="fix">44953 - Extensive fixes for data validation</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action>
<action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action>
<action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action>
<action dev="POI-DEVELOPERS" type="fix">44953 - Extensive fixes for data validation</action>

View File

@ -19,8 +19,11 @@ package org.apache.poi.hssf.extractor;
import java.io.IOException;
import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.hssf.usermodel.HeaderFooter;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
@ -89,6 +92,13 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
}
}
// Header text, if there is any
if(sheet.getHeader() != null) {
text.append(
extractHeaderFooter(sheet.getHeader())
);
}
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
for(int j=firstRow;j<=lastRow;j++) {
@ -154,8 +164,37 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
// Finish off the row
text.append("\n");
}
// Finally Feader text, if there is any
if(sheet.getFooter() != null) {
text.append(
extractHeaderFooter(sheet.getFooter())
);
}
}
return text.toString();
}
private String extractHeaderFooter(HeaderFooter hf) {
StringBuffer text = new StringBuffer();
if(hf.getLeft() != null) {
text.append(hf.getLeft());
}
if(hf.getCenter() != null) {
if(text.length() > 0)
text.append("\t");
text.append(hf.getCenter());
}
if(hf.getRight() != null) {
if(text.length() > 0)
text.append("\t");
text.append(hf.getRight());
}
if(text.length() > 0)
text.append("\n");
return text.toString();
}
}

View File

@ -32,7 +32,7 @@ import org.apache.poi.hssf.record.FooterRecord;
* <P>
* @author Shawn Laubach (slaubach at apache dot org)
*/
public class HSSFFooter extends Object {
public class HSSFFooter extends Object implements HeaderFooter {
FooterRecord footerRecord;
String left;

View File

@ -32,7 +32,7 @@ import org.apache.poi.hssf.record.HeaderRecord;
*
* @author Shawn Laubach (slaubach at apache dot org)
*/
public class HSSFHeader
public class HSSFHeader implements HeaderFooter
{
HeaderRecord headerRecord;

View File

@ -0,0 +1,33 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
/**
* Common interface for {@link HSSFHeader} and
* {@link HSSFFooter}.
*/
public interface HeaderFooter {
public String getLeft();
public void setLeft( String newLeft );
public String getCenter();
public void setCenter( String newCenter );
public String getRight();
public void setRight( String newRight );
}

View File

@ -255,4 +255,20 @@ public final class TestExcelExtractor extends TestCase {
ex.getText());
assertEquals("Excel With Embeded", ex.getSummaryInformation().getTitle());
}
/**
* Test that we get text from headers and footers
*/
public void test45538() throws Exception {
String[] files = new String[] {
"45538_classic_Footer.xls", "45538_form_Footer.xls",
"45538_classic_Header.xls", "45538_form_Header.xls"
};
for(int i=0; i<files.length; i++) {
ExcelExtractor extractor = createExtractor(files[i]);
String text = extractor.getText();
assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
}
}
}