Skip to content

test reading line breaks #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions src/test/java/com/monitorjbl/xlsx/StreamingSheetTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.monitorjbl.xlsx;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import java.util.Iterator;
import java.util.Locale;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;

public class StreamingSheetTest {
@BeforeAll
Expand Down Expand Up @@ -56,4 +53,59 @@ public void testEmptyCellShouldHaveGeneralStyle() throws Exception {
}
}

@Test
public void testCellWithLineBreak() throws IOException {
final String testValue = "1\n2\r\n3";
try (
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
ByteArrayOutputStream bos = new ByteArrayOutputStream()
) {
Sheet xssfSheet = xssfWorkbook.createSheet();
xssfSheet.createRow(0).createCell(0).setCellValue(testValue);

xssfWorkbook.write(bos);

try (Workbook wb = StreamingReader.builder().open(new ByteArrayInputStream(bos.toByteArray()))) {
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
if (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
Cell cell0 = row.getCell(0);
assertNotNull(cell0);
assertEquals(testValue, cell0.getStringCellValue());
}
}
}
}
}

@Test
public void testCellWithLineBreakNoSharedStrings() throws IOException {
//SXSSFWorkbook does not use SharedStrings by default
final String testValue = "1\n2\r\n3";
try (
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
ByteArrayOutputStream bos = new ByteArrayOutputStream()
) {
Sheet xssfSheet = sxssfWorkbook.createSheet();
xssfSheet.createRow(0).createCell(0).setCellValue(testValue);

sxssfWorkbook.write(bos);

try (Workbook wb = StreamingReader.builder().open(new ByteArrayInputStream(bos.toByteArray()))) {
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
if (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
Cell cell0 = row.getCell(0);
assertNotNull(cell0);
assertEquals(testValue, cell0.getStringCellValue());
}
}
}
}
}

}