|
7 | 7 | import org.apache.poi.ss.util.CellAddress;
|
8 | 8 | import org.apache.poi.ss.util.CellRangeAddress;
|
9 | 9 | import org.apache.poi.ss.util.PaneInformation;
|
| 10 | +import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
10 | 11 | import org.apache.poi.xssf.usermodel.XSSFHyperlink;
|
11 | 12 | import org.apache.poi.xssf.usermodel.XSSFSheet;
|
12 | 13 | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
@@ -450,4 +451,59 @@ public void testRowStyle() throws IOException {
|
450 | 451 | assertTrue(row1.isFormatted());
|
451 | 452 | }
|
452 | 453 | }
|
| 454 | + |
| 455 | + @Test |
| 456 | + public void testCellWithLineBreak() throws IOException { |
| 457 | + final String testValue = "1\n2\r\n3"; |
| 458 | + try ( |
| 459 | + XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); |
| 460 | + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() |
| 461 | + ) { |
| 462 | + Sheet xssfSheet = xssfWorkbook.createSheet(); |
| 463 | + xssfSheet.createRow(0).createCell(0).setCellValue(testValue); |
| 464 | + |
| 465 | + xssfWorkbook.write(bos); |
| 466 | + |
| 467 | + try (Workbook wb = StreamingReader.builder().open(bos.toInputStream())) { |
| 468 | + Sheet sheet = wb.getSheetAt(0); |
| 469 | + Iterator<Row> rowIterator = sheet.rowIterator(); |
| 470 | + if (rowIterator.hasNext()) { |
| 471 | + Row row = rowIterator.next(); |
| 472 | + if (row.getRowNum() == 0) { |
| 473 | + Cell cell0 = row.getCell(0); |
| 474 | + assertNotNull(cell0); |
| 475 | + assertEquals(testValue, cell0.getStringCellValue()); |
| 476 | + } |
| 477 | + } |
| 478 | + } |
| 479 | + } |
| 480 | + } |
| 481 | + |
| 482 | + @Test |
| 483 | + public void testCellWithLineBreakNoSharedStrings() throws IOException { |
| 484 | + //SXSSFWorkbook does not use SharedStrings by default |
| 485 | + final String testValue = "1\n2\r\n3"; |
| 486 | + try ( |
| 487 | + SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(); |
| 488 | + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() |
| 489 | + ) { |
| 490 | + Sheet xssfSheet = sxssfWorkbook.createSheet(); |
| 491 | + xssfSheet.createRow(0).createCell(0).setCellValue(testValue); |
| 492 | + |
| 493 | + sxssfWorkbook.write(bos); |
| 494 | + |
| 495 | + try (Workbook wb = StreamingReader.builder().open(bos.toInputStream())) { |
| 496 | + Sheet sheet = wb.getSheetAt(0); |
| 497 | + Iterator<Row> rowIterator = sheet.rowIterator(); |
| 498 | + if (rowIterator.hasNext()) { |
| 499 | + Row row = rowIterator.next(); |
| 500 | + if (row.getRowNum() == 0) { |
| 501 | + Cell cell0 = row.getCell(0); |
| 502 | + assertNotNull(cell0); |
| 503 | + assertEquals(testValue, cell0.getStringCellValue()); |
| 504 | + } |
| 505 | + } |
| 506 | + } |
| 507 | + } |
| 508 | + } |
453 | 509 | }
|
0 commit comments