]> source.dussan.org Git - poi.git/commitdiff
[bug-64732] add support for new line escaping when updating table names - test case
authorPJ Fanning <fanningpj@apache.org>
Tue, 21 Dec 2021 15:16:46 +0000 (15:16 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 21 Dec 2021 15:16:46 +0000 (15:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896247 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java

index 212747c2d972621dd83afe016c3198cf223b266e..10a31e46a8ecfc64b52c51e914de63b473ca022a 100644 (file)
@@ -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<XSSFTable> tables = wb2Sheet.getTables();
+                assertEquals(1, tables.size());
+                XSSFTable wb2Table = tables.get(0);
+                List<XSSFTableColumn> tabColumns = wb2Table.getColumns();
+                assertEquals(2, tabColumns.size());
+                assertEquals("Column1_x000a_with a line break", tabColumns.get(0).getName());
+            }
+        }
+    }
 }