diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 5c6f1670f1..d73ed34dfb 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -178,6 +178,20 @@ public final class TestXSSFSheet extends BaseTestXSheet { } } + @Test + void testHeaderWithAmpersand() throws IOException { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("AmpersandHeader.xlsx")) { + XSSFSheet s = wb.getSheetAt(0); + XSSFOddHeader hdr = (XSSFOddHeader) s.getHeader(); + assertEquals("one && two &&&&", hdr.getCenter()); + hdr.setAreFieldsStripped(true); + + // In Excel headers fields start with '&' + // For '&' to appear as text it needs to be escaped as '&&' + assertEquals("one & two &&", hdr.getCenter()); + } + } + @Test void getAllHeadersFooters() throws IOException { try (XSSFWorkbook workbook = new XSSFWorkbook()) { diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java index 6238a41713..f5c1054849 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java @@ -132,7 +132,7 @@ class TestXSSFHeaderFooter { assertEquals(simple, XSSFOddHeader.stripFields(withPage)); assertEquals(simple, XSSFOddHeader.stripFields(withLots)); assertEquals(simple, XSSFOddHeader.stripFields(withFont)); - assertEquals(simple + "&&", XSSFOddHeader.stripFields(withOtherAnds)); + assertEquals(simple + "&", XSSFOddHeader.stripFields(withOtherAnds)); assertEquals(simple + "&a&b", XSSFOddHeader.stripFields(withOtherAnds2)); // Now test the default strip flag 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 Binary files a/poi/src/test/java9/module-info.class and b/poi/src/test/java9/module-info.class differ diff --git a/test-data/spreadsheet/AmpersandHeader.xls b/test-data/spreadsheet/AmpersandHeader.xls new file mode 100644 index 0000000000..635ea0e2f4 Binary files /dev/null and b/test-data/spreadsheet/AmpersandHeader.xls differ diff --git a/test-data/spreadsheet/AmpersandHeader.xlsx b/test-data/spreadsheet/AmpersandHeader.xlsx new file mode 100644 index 0000000000..b305f31259 Binary files /dev/null and b/test-data/spreadsheet/AmpersandHeader.xlsx differ