Skip to content
Merged
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
}

group = 'io.github.hee9841.excel'
version = '0.0.3'
version = '0.0.4'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class DefaultExcelExporter<T> extends SXSSFExporter<T> {
private final int maxRowsPerSheet;

private SheetStrategy sheetStrategy;
private int currentSheetIndex;

private Sheet currentSheet;


/**
Expand All @@ -66,7 +67,6 @@ public class DefaultExcelExporter<T> extends SXSSFExporter<T> {
super();
this.maxRowsPerSheet = maxRowsPerSheet;
this.sheetName = sheetName;
this.currentSheetIndex = 0;
setSheetStrategy(sheetStrategy);

this.initialize(type, data);
Expand Down Expand Up @@ -136,18 +136,18 @@ protected void validate(Class<?> type, List<T> data) {
*/
@Override
protected void createExcel(List<T> data) {

currentSheet = createNewSheet(sheetName, 0);
createHeader(currentSheet, ROW_START_INDEX);

// 1. If data is empty, create createHeader only.
if (data.isEmpty()) {
Sheet newSheet = createNewSheet();
createHeader(newSheet, ROW_START_INDEX);
logger.warn("Empty data provided - Excel file will be created with headers only.");
return;
}

//2. Add rows
Sheet newSheet = createNewSheet();
createHeader(newSheet, ROW_START_INDEX);
addRows(newSheet, data);
//2. Add Rows
addRows(data);
}

/**
Expand All @@ -163,10 +163,10 @@ protected void createExcel(List<T> data) {
* @throws ExcelException if ONE_SHEET strategy is used and data exceeds max rows limit
*/
@Override
public void addRows(Sheet sheet, List<T> data) {
public void addRows(List<T> data) {
int leftDataSize = data.size();
for (Object renderedData : data) {
createBody(sheet, renderedData, currentRowIndex++);
createBody(currentSheet, renderedData, currentRowIndex++);
leftDataSize--;
if (currentRowIndex == maxRowsPerSheet && leftDataSize > 0) {
//If one sheet strategy, throw exception
Expand All @@ -178,8 +178,8 @@ public void addRows(Sheet sheet, List<T> data) {

//If multi sheet strategy, create new sheet
currentRowIndex = ROW_START_INDEX;
sheet = createNewSheet();
createHeader(sheet, ROW_START_INDEX);
currentSheet = createNewSheet(sheetName, workbook.getSheetIndex(currentSheet) + 1);
createHeader(currentSheet, ROW_START_INDEX);
}
}
}
Expand All @@ -195,28 +195,4 @@ protected void createHeader(Sheet sheet, Integer headerRowIndex) {
super.createHeader(sheet, headerRowIndex);
currentRowIndex++;
}


/**
* Creates a new sheet with headers.
*
* <p>This method resets the current row index, creates a new sheet, and adds headers to it.
* If a sheet name is provided, it will be used as a base name with an index(index starts from
* 0) suffix.</p>
*/
private Sheet createNewSheet() {
//If sheet name is provided, create sheet with sheet name + idx
final String finalSheetName = (sheetName != null)
? String.format("%s%d", sheetName, currentSheetIndex++)
: null;

Sheet sheet = (finalSheetName != null)
? workbook.createSheet(finalSheetName)
: workbook.createSheet();

logger.debug("Create new Sheet : {}.", sheet.getSheetName());

return sheet;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public interface ExcelExporter<T> {
*
* @param data The list of data objects to be added as rows
*/
void addRows(Sheet sheet, List<T> data);
void addRows(List<T> data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ protected void createHeader(Sheet sheet, Integer headerRowIndex) {
}
}

/**
* Creates a new sheet with headers.
*
* <p>This method resets the current row index, creates a new sheet, and adds headers to it.
* If a sheet name is provided, it will be used as a base name with an index(index starts from
* 0) suffix.</p>
*/
protected Sheet createNewSheet(String sheetName, int sheetIndex) {
//If sheet name is provided, create sheet with sheet name + idx
final String finalSheetName = (sheetName != null)
? String.format("%s(%d)", sheetName, sheetIndex)
: null;


Sheet sheet = (finalSheetName != null)
? workbook.createSheet(finalSheetName)
: workbook.createSheet();

logger.debug("Create new Sheet : {}.", sheet.getSheetName());

return sheet;
}

/**
* Creates a row in the Excel sheet for the given data object.
* This method handles field access and cell value setting based on column mapping information.
Expand Down Expand Up @@ -133,9 +156,8 @@ public final void write(OutputStream stream) throws IOException {
}
logger.info("Start to write Excel file for DTO class({}.java).", dtoTypeName);

try (SXSSFWorkbook autoCloseableWb = this.workbook;
OutputStream autoCloseableStream = stream) {
autoCloseableWb.write(autoCloseableStream);
try (SXSSFWorkbook autoCloseableWb = this.workbook) {
autoCloseableWb.write(stream);
logger.info("Successfully wrote Excel file for DTO class({}.java).", dtoTypeName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ void createSheetWithSpecifiedName() throws IOException {
//then
try (Workbook workbook = WorkbookFactory.create(
new ByteArrayInputStream(os.toByteArray()))) {
assertEquals("TestSheet0", workbook.getSheetAt(0).getSheetName());
assertEquals("TestSheet1", workbook.getSheetAt(1).getSheetName());
assertEquals("TestSheet(0)", workbook.getSheetAt(0).getSheetName());
assertEquals("TestSheet(1)", workbook.getSheetAt(1).getSheetName());
}

assertTrue(memoryAppender.isPresent("Create new Sheet : TestSheet0.", Level.DEBUG));
assertTrue(memoryAppender.isPresent("Create new Sheet : TestSheet1.", Level.DEBUG));
assertTrue(memoryAppender.isPresent("Create new Sheet : TestSheet(0).", Level.DEBUG));
assertTrue(memoryAppender.isPresent("Create new Sheet : TestSheet(1).", Level.DEBUG));

}
}
Expand Down Expand Up @@ -308,7 +308,7 @@ void checkFormulaType() throws IOException {
// then
try (Workbook workbook = WorkbookFactory
.create(new ByteArrayInputStream(os.toByteArray()))) {
Sheet sheet = workbook.getSheet("TestSheet0");
Sheet sheet = workbook.getSheetAt(0);

for (int i = 1; i < 10; i++) {
Row row = sheet.getRow(i);
Expand Down