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

hee9841 / excel-module / #38

25 Apr 2025 09:07AM UTC coverage: 82.791%. Remained the same
#38

push

github

web-flow
Release 0.0.1 (#58)

* Docs: 초기 배포를 위한 docs 추가 (#21)

* Docs: README 추가

* Chore: gitignore에 maven pushing에 사용할 설정 파일 추가

* Deploy: Coveralls 추가, CI 추가 (#22)

* Release 0.0.1

* Deploy: 배포시 Readme 버전 설정 추가 (#25)

* Deploy: 베포시 readme 버전 설정

* Fix: 테스트로 추가했던 dev 브런치 삭제

* Docs: javadocs 관련 의존성 추가 및 배포된 maven repository url 추가 (#30)

* Docs: javaDocs 만들기 위한 추가 의존성 추가

* Docs: Maven Central url README에 추가

* Fix: slf4j-api 의존성의 implementation으로 변경 (#33)

* Feat: 로그 info 메세지 변경 (#34)

* Feat: 로깅 메세지 변경

* Feat: 로그 info 메세지 변경(dto 클래스명 SimpleName에서 패키지 포함으로 변경)

* Feat: Excel, ExcelColumn 어노테이션 정합성 예외 처리 (#35)

* Feat: SystemValues 클레스에 ExcelColumn에 허용되는 타입들 추가

* Feat: Excel, ExcelColumn 어노테이션 관련 적합성에 대한 예외 처리 추가

* Fix: ExcelColumnAnnotationProcessor에 AutoService import

* Fix: Supported source version에 대한 경고 제거 (#36)

* Fix: AutoService 제거

- 의존성 삭제
- 프로세서에 어노테이션 제거

* Fix: 어노테이션 프로세서 SourceVersion을 latestSupported로 변경

* Fix: poi 의존성을 implementation에서 api로 변경 (#38)

* Fix: 에러 메세지 수정(STY_CU_003_B, STG_CT_001_B,STG_ID_002_B 테스트 사항) (#46)

* Fix: 에러 메세지 수정(STY_CU_003_B, STG_CT_001_B,STG_ID_002_B 테스트 사항)

* Style: comment, 및 포멧 수정

* Fix: compile 옵션 변경, toolchain java 버전 변경 (#47)

* Chore: gradle 버전 변경 gradle-8.5 -> gradle-8.10

* Chore: java compile source, target 8 에서 release 8로 변경

- toolchain을 23으로 변경

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

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

* Refactor: VerticalAlignment의 default 값을 CENTER로 설정 (#49)

* Refactor: poi 라이브러리의 클래스명과 겹치는 클래스명 변경 (#50)

* Rename: IndexedColor 관련 클래스명 rename

- IndexedColors.java ->  ColorPalette.java
- IndexedExcelColor.java -> PaletteExcelColor.java

* Rename: CellType.java -> ColumnDataType.java으로 클래스명 변경

* Rename: Alignment 관련 Enum 클래스 'Excel' 접두사 추가

* Rename: BorderStyle.java -> ExcelBorderStyle.java로 변경

* Refactor: Excel, ExcelColumn 어노테이션 프로세서 추가 구현 (#52)

* Refactor: ExcelAnnotationProcessor와 ExcelColumnAn... (continued)

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

29 existing lines in 7 files now uncovered.

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

96.67
/src/main/java/io/github/hee9841/excel/meta/ColumnDataType.java
1
package io.github.hee9841.excel.meta;
2

3
import io.github.hee9841.excel.annotation.ExcelColumnStyle;
4
import io.github.hee9841.excel.exception.ExcelException;
5
import io.github.hee9841.excel.format.CellFormats;
6
import java.time.LocalDate;
7
import java.time.LocalDateTime;
8
import java.util.Arrays;
9
import java.util.Collections;
10
import java.util.Date;
11
import java.util.List;
12
import java.util.function.BiConsumer;
13
import org.apache.poi.ss.usermodel.Cell;
14

15
/**
16
 * Enum defining the supported cell types for Excel export/import operations.
17
 * Each type defines how to convert Java values to Excel cell values and which Java types are
18
 * supported.
19
 * The enum also provides utility methods for type matching and cell value setting.
20
 *
21
 * @see ColumnInfoMapper
22
 * @see ColumnInfo
23
 * @see io.github.hee9841.excel.annotation.Excel
24
 * @see io.github.hee9841.excel.annotation.ExcelColumn
25
 * @see ExcelColumnStyle
26
 * @see CellFormats
27
 */
28
public enum ColumnDataType {
1✔
29
    /**
30
     * Automatically determine the cell type based on the field type
31
     */
32
    AUTO,
1✔
33
    /**
34
     * No specific cell type (default)
35
     */
36
    _NONE,
1✔
37
    /**
38
     * Numeric cell type for various number formats
39
     */
40
    NUMBER(
1✔
41
        (cell, o) -> cell.setCellValue(Double.parseDouble(String.valueOf(o))),
1✔
42
        Collections.unmodifiableList(
1✔
43
            Arrays.asList(
1✔
44
                Integer.TYPE, Double.TYPE,
45
                Float.TYPE, Long.TYPE,
46
                Short.TYPE, Byte.TYPE,
47
                Number.class
48
            )
49
        ),
50
        CellFormats._NONE,
51
        true
52
    ),
53
    /**
54
     * Boolean cell type
55
     */
56
    BOOLEAN(
1✔
57
        (cell, o) -> cell.setCellValue((boolean) o),
1✔
58
        Collections.unmodifiableList(
1✔
59
            Arrays.asList(Boolean.TYPE, Boolean.class)
1✔
60
        ),
61
        CellFormats._NONE,
62
        true
63
    ),
64
    /**
65
     * String cell type for text values
66
     */
67
    STRING(
1✔
68
        (cell, o) -> cell.setCellValue(String.valueOf(o)),
1✔
69
        Collections.unmodifiableList(
1✔
70
            Arrays.asList(String.class, Character.class, char.class)
1✔
71
        ),
72
        CellFormats._NONE,
73
        true
74
    ),
75
    /**
76
     * Enum cell type - uses toString() to get the value
77
     */
78
    ENUM(
1✔
79
        (cell, o) -> cell.setCellValue(o != null ? o.toString() : ""),
1✔
80
        Collections.singletonList(Enum.class),
1✔
81
        CellFormats._NONE,
82
        true
83
    ),
84
    /**
85
     * Formula cell type - value is treated as an Excel formula
86
     */
87
    FORMULA(
1✔
88
        (cell, o) -> cell.setCellFormula(String.valueOf(o)),
1✔
89
        Collections.singletonList(String.class),
1✔
90
        CellFormats._NONE,
91
        false
92
    ),
93

94
    /**
95
     * Date And Time cell type
96
     */
97
    DATE(
1✔
98
        (cell, o) -> cell.setCellValue((Date) o),
1✔
99
        Collections.unmodifiableList(
1✔
100
            Arrays.asList(Date.class, java.sql.Date.class)
1✔
101
        ),
102
        CellFormats.DEFAULT_DATE_FORMAT,
103
        true
104
    ),
105
    LOCAL_DATE(
1✔
106
        (cell, o) -> cell.setCellValue((LocalDate) o),
1✔
107
        Collections.singletonList(LocalDate.class),
1✔
108
        CellFormats.DEFAULT_DATE_FORMAT,
109
        true
110
    ),
111
    LOCAL_DATE_TIME(
1✔
112
        (cell, o) -> cell.setCellValue((LocalDateTime) o),
1✔
113
        Collections.singletonList(LocalDateTime.class),
1✔
114
        CellFormats.DEFAULT_DATE_TIME_FORMAT,
115
        true
116
    );
117

118
    /**
119
     * Function to set a cell's value based on the given object
120
     */
121
    private final BiConsumer<Cell, Object> cellValueSetter;
122
    /**
123
     * List of Java types allowed for this cell type
124
     */
125
    private final List<Class<?>> allowedTypes;
126
    /**
127
     * Default data format pattern by this cell type
128
     */
129
    private final String dataFormatPattern;
130
    /**
131
     * Whether this cell type has high priority when has same allowed types
132
     */
133
    private final boolean hasHighPriority;
134

135

136
    ColumnDataType(
137
        BiConsumer<Cell, Object> cellValueSetter,
138
        List<Class<?>> allowedTypes,
139
        String dataFormatPattern,
140
        boolean hasHighPriority
141
    ) {
1✔
142
        this.cellValueSetter = cellValueSetter;
1✔
143
        this.dataFormatPattern = dataFormatPattern;
1✔
144
        this.allowedTypes = allowedTypes;
1✔
145
        this.hasHighPriority = hasHighPriority;
1✔
146
    }
1✔
147

148
    /**
149
     * Default constructor for special {@link ColumnDataType} (AUTO and
150
     * {@link ColumnDataType#_NONE}).
151
     * Uses a default string value setter, empty allowed types list,
152
     * and no specific format pattern.
153
     */
154
    ColumnDataType() {
155
        this(
1✔
156
            (cell, o) -> cell.setCellValue(String.valueOf(o)),
1✔
157
            Collections.emptyList(),
1✔
158
            CellFormats._NONE,
159
            false
160
        );
161
    }
1✔
162

163
    /**
164
     * Determines the appropriate {@link ColumnDataType} based on the given field type.
165
     * Searches through all {@link ColumnDataType} that have high priority and
166
     * finds the first one where the field type is assignable to one of the allowed types.
167
     *
168
     * @param fieldType The Java type to match against cell types
169
     * @return The matching {@link ColumnDataType}, or {@link ColumnDataType#_NONE} if no match is found
170
     */
171
    public static ColumnDataType from(Class<?> fieldType) {
172
        return Arrays.stream(values())
1✔
173
            .filter(
1✔
174
                cellType -> cellType.hasHighPriority &&
1✔
175
                    cellType.allowedTypes.stream().anyMatch(c -> c.isAssignableFrom(fieldType))
1✔
176
            )
177
            .findFirst()
1✔
178
            .orElse(_NONE);
1✔
179
    }
180

181
    /**
182
     * Checks if fieldType matches one of the allowedTypes in targetCellType,
183
     * returns targetCellType if matched, otherwise returns {@link ColumnDataType#_NONE}.
184
     *
185
     * @param fieldType            The field type
186
     * @param targetColumnDataType specific {@link ColumnDataType}
187
     * @return Returns targetCellType if matched, otherwise returns {@link ColumnDataType#_NONE}
188
     */
189
    public static ColumnDataType findMatchingCellType(Class<?> fieldType,
190
        ColumnDataType targetColumnDataType) {
191
        return targetColumnDataType.allowedTypes.stream()
1✔
192
            .anyMatch(c -> c.isAssignableFrom(fieldType)) ? targetColumnDataType : _NONE;
1✔
193
    }
194

195
    /**
196
     * Sets a cell's value according to this {@link ColumnDataType}.
197
     * If the value is null, an empty string will be set.
198
     *
199
     * @param cell  The Excel cell to set the value for
200
     * @param value The value to set in the cell
201
     * @throws ExcelException If the value cannot be set for any reason
202
     */
203
    public void setCellValueByCellType(Cell cell, Object value) {
204
        if (value == null) {
1✔
205
            _NONE.cellValueSetter.accept(cell, "");
1✔
206
            return;
1✔
207
        }
208
        try {
209
            cellValueSetter.accept(cell, value);
1✔
UNCOV
210
        } catch (Exception e) {
×
UNCOV
211
            throw new ExcelException("Failed to set cell value by cell type: " + e.getMessage());
×
212
        }
1✔
213
    }
1✔
214

215

216
    public boolean isAuto() {
217
        return this == AUTO;
1✔
218
    }
219

220

221
    public boolean isNone() {
222
        return this == _NONE;
1✔
223
    }
224

225
    public String getDataFormatPattern() {
226
        return dataFormatPattern;
1✔
227
    }
228

229
}
230

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