diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 52c7218..df1d3ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: uses: actions/setup-java@v3 with: distribution: 'temurin' - java-version: '23' + java-version: '21' - name: Grant execute permission for Gradle run: chmod +x gradlew diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index 980b65c..53d7fc3 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -19,7 +19,7 @@ jobs: - name: Set up JDK 11 uses: actions/setup-java@v4 with: - java-version: '23' + java-version: '21' distribution: 'temurin' cache: 'gradle' diff --git a/build.gradle b/build.gradle index 0b5404f..0f111ca 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { } group = 'io.github.hee9841.excel' -version = '0.0.2' +version = '0.0.3' repositories { mavenCentral() diff --git a/src/main/java/io/github/hee9841/excel/core/exporter/DefaultExcelExporter.java b/src/main/java/io/github/hee9841/excel/core/exporter/DefaultExcelExporter.java index a75fe65..931be4b 100644 --- a/src/main/java/io/github/hee9841/excel/core/exporter/DefaultExcelExporter.java +++ b/src/main/java/io/github/hee9841/excel/core/exporter/DefaultExcelExporter.java @@ -4,6 +4,7 @@ import io.github.hee9841.excel.strategy.SheetStrategy; import java.text.MessageFormat; import java.util.List; +import org.apache.poi.ss.usermodel.Sheet; /** * DefaultExcelExporter is a concrete implementation of {@link SXSSFExporter} that provides functionality @@ -69,7 +70,7 @@ public class DefaultExcelExporter extends SXSSFExporter { setSheetStrategy(sheetStrategy); this.initialize(type, data); - this.createExcelFile(data); + this.createExcel(data); } @@ -85,6 +86,22 @@ public static DefaultExcelExporterBuilder builder(Class type, List return new DefaultExcelExporterBuilder<>(type, data, supplyExcelVersion.getMaxRows()); } + /** + * Sets the sheet strategy for this exporter. + * + *

This method also configures the workbook's Zip64 mode based on the selected strategy.

+ * + * @param strategy The sheet strategy to use (ONE_SHEET or MULTI_SHEET)- + */ + private void setSheetStrategy(SheetStrategy strategy) { + + this.sheetStrategy = strategy; + workbook.setZip64Mode(sheetStrategy.getZip64Mode()); + + logger.debug("Set sheet strategy and Zip64Mode - strategy: {}, Zip64Mode: {}.", + strategy.name(), sheetStrategy.getZip64Mode().name()); + } + /** * Validates the data size against the maximum rows per sheet limit. @@ -107,7 +124,7 @@ protected void validate(Class type, List data) { } /** - * Creates the Excel file with the provided data. + * Creates the Excel(workBook) with the provided data. * *

This method handles the creation of sheets and rows based on the data:

*
    @@ -118,17 +135,19 @@ protected void validate(Class type, List data) { * @param data The list of data objects to be exported */ @Override - protected void createExcelFile(List data) { + protected void createExcel(List data) { // 1. If data is empty, create createHeader only. if (data.isEmpty()) { - createNewSheetWithHeader(); + 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 - createNewSheetWithHeader(); - addRows(data); + Sheet newSheet = createNewSheet(); + createHeader(newSheet, ROW_START_INDEX); + addRows(newSheet, data); } /** @@ -144,10 +163,10 @@ protected void createExcelFile(List data) { * @throws ExcelException if ONE_SHEET strategy is used and data exceeds max rows limit */ @Override - public void addRows(List data) { + public void addRows(Sheet sheet, List data) { int leftDataSize = data.size(); for (Object renderedData : data) { - createBody(renderedData, currentRowIndex++); + createBody(sheet, renderedData, currentRowIndex++); leftDataSize--; if (currentRowIndex == maxRowsPerSheet && leftDataSize > 0) { //If one sheet strategy, throw exception @@ -158,27 +177,26 @@ public void addRows(List data) { } //If multi sheet strategy, create new sheet - createNewSheetWithHeader(); + currentRowIndex = ROW_START_INDEX; + sheet = createNewSheet(); + createHeader(sheet, ROW_START_INDEX); } } } /** - * Sets the sheet strategy for this exporter. + * Override createHeader Method to add currentRowIndex. * - *

    This method also configures the workbook's Zip64 mode based on the selected strategy.

    - * - * @param strategy The sheet strategy to use (ONE_SHEET or MULTI_SHEET)- + * @param sheet The sheet to add headers to + * @param headerRowIndex The headers row index */ - private void setSheetStrategy(SheetStrategy strategy) { - - this.sheetStrategy = strategy; - workbook.setZip64Mode(sheetStrategy.getZip64Mode()); - - logger.debug("Set sheet strategy and Zip64Mode - strategy: {}, Zip64Mode: {}.", - strategy.name(), sheetStrategy.getZip64Mode().name()); + @Override + protected void createHeader(Sheet sheet, Integer headerRowIndex) { + super.createHeader(sheet, headerRowIndex); + currentRowIndex++; } + /** * Creates a new sheet with headers. * @@ -186,19 +204,19 @@ private void setSheetStrategy(SheetStrategy strategy) { * If a sheet name is provided, it will be used as a base name with an index(index starts from * 0) suffix.

    */ - private void createNewSheetWithHeader() { - currentRowIndex = ROW_START_INDEX; - + private Sheet createNewSheet() { //If sheet name is provided, create sheet with sheet name + idx - if (sheetName != null) { - sheet = workbook.createSheet(String.format("%s%d", sheetName, currentSheetIndex++)); - } else { - sheet = workbook.createSheet(); - } + 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()); - createHeaderWithNewSheet(sheet, ROW_START_INDEX); - currentRowIndex++; + return sheet; } + } diff --git a/src/main/java/io/github/hee9841/excel/core/exporter/ExcelExporter.java b/src/main/java/io/github/hee9841/excel/core/exporter/ExcelExporter.java index 30903d5..40aac19 100644 --- a/src/main/java/io/github/hee9841/excel/core/exporter/ExcelExporter.java +++ b/src/main/java/io/github/hee9841/excel/core/exporter/ExcelExporter.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; +import org.apache.poi.ss.usermodel.Sheet; /** * Core interface for Excel file operations in the library. @@ -31,5 +32,5 @@ public interface ExcelExporter { * * @param data The list of data objects to be added as rows */ - void addRows(List data); + void addRows(Sheet sheet, List data); } diff --git a/src/main/java/io/github/hee9841/excel/core/exporter/SXSSFExporter.java b/src/main/java/io/github/hee9841/excel/core/exporter/SXSSFExporter.java index 3b6c0d1..1710fbb 100644 --- a/src/main/java/io/github/hee9841/excel/core/exporter/SXSSFExporter.java +++ b/src/main/java/io/github/hee9841/excel/core/exporter/SXSSFExporter.java @@ -1,8 +1,8 @@ package io.github.hee9841.excel.core.exporter; -import io.github.hee9841.excel.exception.ExcelException; import io.github.hee9841.excel.core.meta.ColumnInfo; import io.github.hee9841.excel.core.meta.ColumnInfoMapper; +import io.github.hee9841.excel.exception.ExcelException; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; @@ -44,7 +44,6 @@ public abstract class SXSSFExporter implements ExcelExporter { protected SXSSFWorkbook workbook; protected Map columnsMappingInfo; - protected Sheet sheet; protected String dtoTypeName; @@ -73,34 +72,15 @@ protected void initialize(Class type, List data) { this.columnsMappingInfo = ColumnInfoMapper.of(type, workbook).map(); } - /** - * Validates the provided data and type. - * This method can be overridden by subclasses to add custom validation logic. - * - * @param type The class of the data type - * @param data The list of data objects to be exported - */ - protected void validate(Class type, List data) { - } - - /** - * Creates the Excel file with the provided data. - * This method must be implemented by subclasses to define their specific sheet management - * strategy. - * - * @param data The list of data objects to be exported - */ - protected abstract void createExcelFile(List data); /** - * Creates headers for a new sheet using the column mapping information. + * Creates headers using the column mapping information. * - * @param newSheet The sheet to add headers to - * @param startRowIndex The row index where headers should start + * @param sheet The sheet to add headers to + * @param headerRowIndex The headers row index */ - protected void createHeaderWithNewSheet(Sheet newSheet, Integer startRowIndex) { - this.sheet = newSheet; - Row row = sheet.createRow(startRowIndex); + protected void createHeader(Sheet sheet, Integer headerRowIndex) { + Row row = sheet.createRow(headerRowIndex); for (Integer colIndex : columnsMappingInfo.keySet()) { ColumnInfo columnMappingInfo = columnsMappingInfo.get(colIndex); Cell cell = row.createCell(colIndex); @@ -113,11 +93,12 @@ protected void createHeaderWithNewSheet(Sheet newSheet, Integer startRowIndex) { * 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. * - * @param data The data object to create a row for + * @param sheet The Sheet object to create a row. + * @param data The data object for rendering data to cell * @param rowIndex The index of the row to create * @throws ExcelException if field access fails */ - protected void createBody(Object data, int rowIndex) { + protected void createBody(Sheet sheet, Object data, int rowIndex) { logger.debug("Add rows data - row:{}.", rowIndex); Row row = sheet.createRow(rowIndex); for (Integer colIndex : columnsMappingInfo.keySet()) { @@ -146,7 +127,7 @@ protected void createBody(Object data, int rowIndex) { * @throws IOException if an I/O error occurs during writing */ @Override - public void write(OutputStream stream) throws IOException { + public final void write(OutputStream stream) throws IOException { if (stream == null) { throw new ExcelException("Output stream is null."); } @@ -159,4 +140,22 @@ public void write(OutputStream stream) throws IOException { } } + /** + * Validates the provided data and type. + * This method can be overridden by subclasses to add custom validation logic. + * + * @param type The class of the data type + * @param data The list of data objects to be exported + */ + protected abstract void validate(Class type, List data); + + /** + * Creates the Excel file with the provided data. + * This method must be implemented by subclasses to define their specific sheet management + * strategy. + * + * @param data The list of data objects to be exported + */ + protected abstract void createExcel(List data); + }