From: PJ Fanning Date: Tue, 21 Dec 2021 15:16:46 +0000 (+0000) Subject: [bug-64732] add support for new line escaping when updating table names - test case X-Git-Tag: REL_5_2_0~59 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a2dbbd774862a1a9d8469f99cebcaeb1697e415e;p=poi.git [bug-64732] add support for new line escaping when updating table names - test case git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896247 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java index 212747c2d9..10a31e46a8 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java @@ -34,6 +34,7 @@ import java.util.Locale; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.TempFile; @@ -653,4 +654,58 @@ public final class TestXSSFTable { final AreaReference area = new AreaReference(upperLeft, lowerRight, SpreadsheetVersion.EXCEL2007); return sheet.createTable(area); } + + @Test + void testNamesWithNewLines() throws IOException { + try ( + XSSFWorkbook wb = new XSSFWorkbook(); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + XSSFSheet sheet = wb.createSheet(); + + // headers + XSSFRow headersRow = sheet.createRow(0); + headersRow.createCell(0).setCellValue("Column1"); + headersRow.createCell(1).setCellValue("Column2"); + + // a second row + XSSFRow row = sheet.createRow(1); + row.createCell(0).setCellValue(1); + row.createCell(1).setCellValue(2); + + // create a table + AreaReference area = wb.getCreationHelper().createAreaReference( + new CellReference(sheet.getRow(0).getCell(0)), + new CellReference(sheet.getRow(1).getCell(1)) + ); + XSSFTable table = sheet.createTable(area); + + // styling (no problem here) + sheet.setColumnWidth(0, 5000); + sheet.setColumnWidth(1, 5000); + CTTable cttable = table.getCTTable(); + cttable.addNewTableStyleInfo(); + XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle(); + style.setName("TableStyleMedium6"); + style.setShowColumnStripes(false); + style.setShowRowStripes(true); + cttable.addNewAutoFilter().setRef(area.formatAsString()); + CellStyle cellStyle = wb.createCellStyle(); + cellStyle.setWrapText(true); + headersRow.getCell(0).setCellStyle(cellStyle); + headersRow.getCell(0).setCellValue("Column1\nwith a line break"); + + wb.write(bos); + + try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) { + XSSFSheet wb2Sheet = wb2.getSheetAt(0); + List tables = wb2Sheet.getTables(); + assertEquals(1, tables.size()); + XSSFTable wb2Table = tables.get(0); + List tabColumns = wb2Table.getColumns(); + assertEquals(2, tabColumns.size()); + assertEquals("Column1_x000a_with a line break", tabColumns.get(0).getName()); + } + } + } }