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

hee9841 / excel-module / #22

09 Apr 2025 06:28AM UTC coverage: 82.732% (-0.1%) from 82.835%
#22

push

github

web-flow
Refactor: ExcelExporter의 validate 메서드 추가(data size, sheet Strategy에 따른) (#48)

- validate 오버라이딩으로 생성자의 setSheetStrategy 메서드호출과 initialize 호출 시점 변경,

7 of 8 new or added lines in 1 file covered. (87.5%)

1 existing line in 1 file now uncovered.

527 of 637 relevant lines covered (82.73%)

0.83 hits per line

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

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

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

41
    protected static final SpreadsheetVersion supplyExcelVersion = SpreadsheetVersion.EXCEL2007;
1✔
42

43
    protected SXSSFWorkbook workbook;
44
    protected Map<Integer, ColumnInfo> columnsMappingInfo;
45
    protected Sheet sheet;
46

47
    protected String dtoTypeName;
48

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

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

67
        validate(type, data);
1✔
68

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

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

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

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

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

138
    /**
139
     * Writes the Excel file content to the specified output stream.
140
     * This method ensures proper resource cleanup using try-with-resources.
141
     *
142
     * @param stream The output stream to write the Excel file to
143
     * @throws IOException if an I/O error occurs during writing
144
     */
145
    @Override
146
    public void write(OutputStream stream) throws IOException {
147
        logger.info("Start to write Excel file for DTO class({}.java).", dtoTypeName);
1✔
148
        try (SXSSFWorkbook autoCloseableWb = this.workbook;
1✔
149
            OutputStream autoCloseableStream = stream) {
1✔
150
            autoCloseableWb.write(autoCloseableStream);
1✔
151
        }
152
        logger.info("Successfully wrote Excel file for DTO class({}.java).", dtoTypeName);
1✔
153
    }
1✔
154
}
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