• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hee9841 / excel-module / #35

25 Apr 2025 09:05AM UTC coverage: 82.791%. First build
#35

Pull #58

github

hee9841
Revert "Chore: 배포를 위한 PR template 추가 (#54)"

This reverts commit bd34b2c5e.
Pull Request #58: Release 0.0.1

105 of 168 new or added lines in 16 files covered. (62.5%)

534 of 645 relevant lines covered (82.79%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.36
/src/main/java/io/github/hee9841/excel/core/SXSSFExcelFile.java
1
package io.github.hee9841.excel.core;
2

3
import io.github.hee9841.excel.exception.ExcelException;
4
import io.github.hee9841.excel.meta.ColumnInfo;
5
import io.github.hee9841.excel.meta.ColumnInfoMapper;
6
import java.io.IOException;
7
import java.io.OutputStream;
8
import java.lang.reflect.Field;
9
import java.util.List;
10
import java.util.Map;
11
import org.apache.commons.lang3.reflect.FieldUtils;
12
import org.apache.poi.ss.SpreadsheetVersion;
13
import org.apache.poi.ss.usermodel.Cell;
14
import org.apache.poi.ss.usermodel.Row;
15
import org.apache.poi.ss.usermodel.Sheet;
16
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

20
/**
21
 * Abstract base class for Excel file operations using Apache POI's SXSSF (Streaming XML Spreadsheet
22
 * Format).
23
 * This class provides the core functionality for handling Excel files with streaming support for
24
 * large datasets.
25
 *
26
 * <p>Key features:</p>
27
 * <ul>
28
 *     <li>Uses SXSSFWorkbook for memory-efficient handling of large Excel files</li>
29
 *     <li>Supports Excel 2007+ format (XLSX)</li>
30
 *     <li>Provides column mapping and header generation</li>
31
 *     <li>Handles cell styling and data type conversion</li>
32
 * </ul>
33
 *
34
 * <p>This class implements the core functionality while leaving sheet management strategies
35
 * to be implemented by concrete subclasses.</p>
36
 *
37
 * @param <T> The type of data to be handled in the Excel file
38
 */
39
public abstract class SXSSFExcelFile<T> implements ExcelFile<T> {
40

41
    protected static final Logger logger = LoggerFactory.getLogger(SXSSFExcelFile.class);
1✔
42

43
    protected static final SpreadsheetVersion supplyExcelVersion = SpreadsheetVersion.EXCEL2007;
1✔
44

45
    protected SXSSFWorkbook workbook;
46
    protected Map<Integer, ColumnInfo> columnsMappingInfo;
47
    protected Sheet sheet;
48

49
    protected String dtoTypeName;
50

51
    /**
52
     * Constructs a new SXSSFExcelFile with a new SXSSFWorkbook instance.
53
     */
54
    protected SXSSFExcelFile() {
1✔
55
        this.workbook = new SXSSFWorkbook();
1✔
56
    }
1✔
57

58
    /**
59
     * Initializes the Excel file with the specified type and data.
60
     * This method performs validation and sets up column mapping information.
61
     *
62
     * @param type The class type of the data to be exported
63
     * @param data The list of data objects to be exported
64
     */
65
    protected void initialize(Class<?> type, List<T> data) {
66
        this.dtoTypeName = type.getName();
1✔
67
        logger.info("Initializing Excel file for DTO: {}.java.", dtoTypeName);
1✔
68

69
        validate(type, data);
1✔
70

71
        logger.debug("Mapping DTO to Excel data - DTO class({}).", dtoTypeName);
1✔
72
        //Map DTO to Excel data
73
        this.columnsMappingInfo = ColumnInfoMapper.of(type, workbook).map();
1✔
74
    }
1✔
75

76
    /**
77
     * Validates the provided data and type.
78
     * This method can be overridden by subclasses to add custom validation logic.
79
     *
80
     * @param type The class of the data type
81
     * @param data The list of data objects to be exported
82
     */
83
    protected void validate(Class<?> type, List<T> data) {
84
    }
×
85

86
    /**
87
     * Creates the Excel file with the provided data.
88
     * This method must be implemented by subclasses to define their specific sheet management
89
     * strategy.
90
     *
91
     * @param data The list of data objects to be exported
92
     */
93
    protected abstract void createExcelFile(List<T> data);
94

95
    /**
96
     * Creates headers for a new sheet using the column mapping information.
97
     *
98
     * @param newSheet      The sheet to add headers to
99
     * @param startRowIndex The row index where headers should start
100
     */
101
    protected void createHeaderWithNewSheet(Sheet newSheet, Integer startRowIndex) {
102
        this.sheet = newSheet;
1✔
103
        Row row = sheet.createRow(startRowIndex);
1✔
104
        for (Integer colIndex : columnsMappingInfo.keySet()) {
1✔
105
            ColumnInfo columnMappingInfo = columnsMappingInfo.get(colIndex);
1✔
106
            Cell cell = row.createCell(colIndex);
1✔
107
            cell.setCellValue(columnMappingInfo.getHeaderName());
1✔
108
            cell.setCellStyle(columnMappingInfo.getHeaderStyle());
1✔
109
        }
1✔
110
    }
1✔
111

112
    /**
113
     * Creates a row in the Excel sheet for the given data object.
114
     * This method handles field access and cell value setting based on column mapping information.
115
     *
116
     * @param data     The data object to create a row for
117
     * @param rowIndex The index of the row to create
118
     * @throws ExcelException if field access fails
119
     */
120
    protected void createBody(Object data, int rowIndex) {
121
        logger.debug("Add rows data - row:{}.", rowIndex);
1✔
122
        Row row = sheet.createRow(rowIndex);
1✔
123
        for (Integer colIndex : columnsMappingInfo.keySet()) {
1✔
124
            ColumnInfo columnInfo = columnsMappingInfo.get(colIndex);
1✔
125
            try {
126
                Field field = FieldUtils.getField(data.getClass(), columnInfo.getFieldName(), true);
1✔
127
                Cell cell = row.createCell(colIndex);
1✔
128
                //Set cell value by cell type
129
                columnInfo.getColumnType().setCellValueByCellType(cell, field.get(data));
1✔
130
                //Set cell style
131
                cell.setCellStyle(columnInfo.getBodyStyle());
1✔
132
            } catch (IllegalAccessException e) {
×
133
                throw new ExcelException(
×
134
                    String.format("Failed to create body(column:%d, row:%d) : "
×
135
                            + "Access to field %s failed.",
136
                        colIndex, rowIndex, columnInfo.getFieldName()), e);
×
137
            }
1✔
138
        }
1✔
139
    }
1✔
140

141
    /**
142
     * Writes the Excel file content to the specified output stream.
143
     * This method ensures proper resource cleanup using try-with-resources.
144
     *
145
     * @param stream The output stream to write the Excel file to
146
     * @throws IOException if an I/O error occurs during writing
147
     */
148
    @Override
149
    public void write(OutputStream stream) throws IOException {
150
        if (stream == null) {
1✔
NEW
151
            throw new ExcelException("Output stream is null.");
×
152
        }
153
        logger.info("Start to write Excel file for DTO class({}.java).", dtoTypeName);
1✔
154

155
        try (SXSSFWorkbook autoCloseableWb = this.workbook;
1✔
156
            OutputStream autoCloseableStream = stream) {
1✔
157
            autoCloseableWb.write(autoCloseableStream);
1✔
158
            logger.info("Successfully wrote Excel file for DTO class({}.java).", dtoTypeName);
1✔
159
        }
160
    }
1✔
161

162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc