aboutsummaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-18 17:15:04 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-18 17:15:04 +0000
commit005810640deb3499a03bbec0cddbacc178bdb2d5 (patch)
tree941e06ad257e19690da9582d55ad320f025e942b /poi
parent5d23faf3cc0ce018b7c50bb9ac5a398c789fff2b (diff)
downloadpoi-005810640deb3499a03bbec0cddbacc178bdb2d5.tar.gz
poi-005810640deb3499a03bbec0cddbacc178bdb2d5.zip
[bug-65638] Remove escaping of ampersand from headers and footers created by Excel. Thanks to Viru.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894357 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/usermodel/HeaderFooter.java11
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java16
-rw-r--r--poi/src/test/java9/module-info.classbin4198 -> 4145 bytes
3 files changed, 22 insertions, 5 deletions
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HeaderFooter.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
index 9d369ad378..aa883dd44e 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
@@ -298,10 +298,13 @@ public abstract class HeaderFooter implements org.apache.poi.ss.usermodel.Header
// Now do the tricky, dynamic ones
// These are things like font sizes, font names and colours
- text = text.replaceAll("\\&\\d+", "");
- text = text.replaceAll("\\&\".*?,.*?\"", "");
- text = text.replaceAll("\\&K[\\dA-F]{6}", "");
- text = text.replaceAll("\\&K[\\d]{2}[+][\\d]{3}", "");
+ text = text.replaceAll("&\\d+", "");
+ text = text.replaceAll("&\".*?,.*?\"", "");
+ text = text.replaceAll("&K[\\dA-F]{6}", "");
+ text = text.replaceAll("&K[\\d]{2}[+][\\d]{3}", "");
+
+ text = text.replaceAll("&&", "&");
+
// All done
return text;
diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java
index 22ee32af31..b703390585 100644
--- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java
+++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java
@@ -69,7 +69,7 @@ final class TestHSSFHeaderFooter {
assertEquals(simple, HSSFHeader.stripFields(withPage));
assertEquals(simple, HSSFHeader.stripFields(withLots));
assertEquals(simple, HSSFHeader.stripFields(withFont));
- assertEquals(simple + "&&", HSSFHeader.stripFields(withOtherAnds));
+ assertEquals(simple + "&", HSSFHeader.stripFields(withOtherAnds));
assertEquals(simple + "&a&b", HSSFHeader.stripFields(withOtherAnds2));
// Now test the default strip flag
@@ -192,4 +192,18 @@ final class TestHSSFHeaderFooter {
assertEquals("bar", footer2.getCenter());
}
}
+
+ @Test
+ void testHeaderWithAmpersand() throws IOException {
+ try (HSSFWorkbook wb = openSampleWorkbook("AmpersandHeader.xls")) {
+ HSSFSheet s = wb.getSheetAt(0);
+ HSSFHeader h = s.getHeader();
+ String header = h.getCenter();
+ assertEquals("one && two &&&&", header);
+
+ // In Excel headers fields start with '&'
+ // For '&' to appear as text it needs to be escaped as '&&'
+ assertEquals("one & two &&", HSSFHeader.stripFields(header));
+ }
+ }
}
diff --git a/poi/src/test/java9/module-info.class b/poi/src/test/java9/module-info.class
index 438e778ab5..028b943d0a 100644
--- a/poi/src/test/java9/module-info.class
+++ b/poi/src/test/java9/module-info.class
Binary files differ