Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import lombok.Setter;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.csv.CSVFormat;
import org.apache.fesod.sheet.exception.ExcelGenerateException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.udf.UDFFinder;
Expand Down Expand Up @@ -170,18 +171,29 @@ public int getSheetIndex(Sheet sheet) {

@Override
public Sheet createSheet() {
assert csvSheet == null : "CSV repeat creation is not allowed.";
checkSheetNotCreated();
csvSheet = new CsvSheet(this, out);
return csvSheet;
}

@Override
public Sheet createSheet(String sheetname) {
assert csvSheet == null : "CSV repeat creation is not allowed.";
checkSheetNotCreated();
csvSheet = new CsvSheet(this, out);
return csvSheet;
}

private void checkSheetNotCreated() {
if (csvSheet != null) {
try {
csvSheet.close();
} catch (IOException e) {
throw new ExcelGenerateException(e);
}
throw new ExcelGenerateException("CSV repeat creation is not allowed.");
}
}

@Override
public Sheet cloneSheet(int sheetNum) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.fesod.sheet.ExcelReader;
import org.apache.fesod.sheet.ExcelWriter;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.exception.ExcelGenerateException;
import org.apache.fesod.sheet.metadata.csv.CsvConstant;
import org.apache.fesod.sheet.metadata.csv.CsvWorkbook;
import org.apache.fesod.sheet.read.metadata.ReadSheet;
Expand Down Expand Up @@ -226,6 +227,31 @@ public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
.doWrite(data);
}

@Test
public void writeSecondCsvSheetShouldFailWithoutLosingFirstSheetData() throws Exception {
File csvFile = createTempFile("csv-repeat-sheet", ExcelFormat.CSV);
List<CsvData> firstSheetData = dataList(3, "First");
List<CsvData> secondSheetData = dataList(3, "Second");

try (ExcelWriter excelWriter = FesodSheet.write(csvFile, CsvData.class)
.excelType(ExcelTypeEnum.CSV)
.build()) {
excelWriter.write(firstSheetData, FesodSheet.writerSheet(0).build());

ExcelGenerateException exception = Assertions.assertThrows(ExcelGenerateException.class, () -> {
excelWriter.write(secondSheetData, FesodSheet.writerSheet(1).build());
});
Assertions.assertEquals("CSV repeat creation is not allowed.", exception.getMessage());
}

List<CsvData> actualData = FesodSheet.read(csvFile, CsvData.class, new CsvDataListener())
.csv()
.doReadSync();
Assertions.assertEquals(3, actualData.size());
Assertions.assertEquals("First0", actualData.get(0).getString());
Assertions.assertEquals("First2", actualData.get(2).getString());
}

private void doTest(
boolean isCreate,
File csvFile,
Expand Down
Loading