[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
This commit is contained in:
PJ Fanning 2021-10-18 17:15:04 +00:00
parent 5d23faf3cc
commit 005810640d
7 changed files with 37 additions and 6 deletions

View File

@ -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()) {

View File

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

View File

@ -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;

View File

@ -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));
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.