Skip to content

Commit bfd3450

Browse files
committed
Optimize code
1 parent a0d5a3f commit bfd3450

File tree

6 files changed

+164
-173
lines changed

6 files changed

+164
-173
lines changed

tang-commons/src/main/java/com/tang/commons/annotation/poi/Excel.java

Lines changed: 0 additions & 167 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.tang.commons.annotation.poi
2+
3+
import org.springframework.core.annotation.AliasFor
4+
5+
import com.tang.commons.enumeration.poi.CellType
6+
import com.tang.commons.enumeration.poi.Type
7+
8+
/**
9+
* Excel 注解
10+
*
11+
* @author Tang
12+
*/
13+
@Target(AnnotationTarget.FIELD)
14+
@Retention(AnnotationRetention.RUNTIME)
15+
@MustBeDocumented
16+
annotation class Excel(
17+
18+
/**
19+
* Alias for [name]
20+
*/
21+
@get:AliasFor("name") val value: String = "",
22+
23+
/**
24+
* 列名称
25+
*/
26+
@get:AliasFor("value") val name: String = "",
27+
28+
/**
29+
* 顺序
30+
*/
31+
val sort: Int = 0,
32+
33+
/**
34+
* 单元格类型
35+
*/
36+
val cellType: CellType = CellType.STRING,
37+
38+
/**
39+
* 日期格式
40+
*/
41+
val dateFormat: String = "yyyy-MM-dd HH:mm:ss",
42+
43+
/**
44+
* 字段类型
45+
*/
46+
val type: Type = Type.ALL,
47+
48+
/**
49+
* 字典类型
50+
*/
51+
val dictType: String = "",
52+
53+
/**
54+
* 列宽
55+
*/
56+
val width: Int = 10,
57+
58+
/**
59+
* 行高
60+
*/
61+
val height: Int = 16,
62+
63+
/**
64+
* 标题行高
65+
*/
66+
val titleHeight: Int = 16
67+
68+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.tang.commons.enumeration.poi
2+
3+
/**
4+
* Excel 单元格类型
5+
*
6+
* @author Tang
7+
*/
8+
enum class CellType(val code: Int) {
9+
10+
/**
11+
* 字符串
12+
*/
13+
STRING(0),
14+
15+
/**
16+
* 数字
17+
*/
18+
NUMBER(1),
19+
20+
/**
21+
* 日期
22+
*/
23+
DATE(2);
24+
25+
companion object {
26+
27+
/**
28+
* 根据 code 返回枚举类型
29+
*
30+
* @param code 单元格类型
31+
* @return [CellType] 单元格类型
32+
*/
33+
fun getCellType(code: Int): CellType? {
34+
for (cellType in CellType.entries) {
35+
if (cellType.code == code) {
36+
return cellType
37+
}
38+
}
39+
return null
40+
}
41+
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.tang.commons.enumeration.poi
2+
3+
/**
4+
* Excel 字段类型
5+
*
6+
* @author Tang
7+
*/
8+
enum class Type(val code: Int) {
9+
10+
/**
11+
* 导入导出
12+
*/
13+
ALL(0),
14+
15+
/**
16+
* 导入
17+
*/
18+
IMPORT(1),
19+
20+
/**
21+
* 导出
22+
*/
23+
EXPORT(2);
24+
25+
companion object {
26+
27+
/**
28+
* 根据 code 返回枚举类型
29+
*
30+
* @param code 导出或导入
31+
* @return [Type] 导出或导入
32+
*/
33+
fun getType(code: Int): Type? {
34+
for (type in Type.entries) {
35+
if (type.code == code) {
36+
return type
37+
}
38+
}
39+
return null
40+
}
41+
42+
}
43+
44+
}

tang-commons/src/main/kotlin/com/tang/commons/utils/poi/ExcelUtils.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.google.common.base.CaseFormat
44
import com.tang.commons.annotation.poi.Excel
55
import com.tang.commons.constants.ContentType
66
import com.tang.commons.constants.FileType
7+
import com.tang.commons.enumeration.poi.Type
8+
import com.tang.commons.enumeration.poi.CellType as ExcelCellType
79
import com.tang.commons.exception.file.FileTypeMismatchException
810
import com.tang.commons.utils.Assert
911
import com.tang.commons.utils.DictUtils
@@ -70,7 +72,7 @@ object ExcelUtils {
7072
val cellNumIndex = AtomicInteger()
7173
fields.forEach { (field: Field, excel: Excel) ->
7274
val cell = row.getCell(cellNumIndex.getAndIncrement())
73-
if (excel.type == Excel.Type.EXPORT) {
75+
if (excel.type == Type.EXPORT) {
7476
return@forEach
7577
}
7678
ReflectionUtils.makeAccessible(field)
@@ -289,7 +291,7 @@ object ExcelUtils {
289291
ReflectionUtils.makeAccessible(field)
290292
val stringValue = Objects.toString(field.get(clazz), "")
291293
when (excel.cellType) {
292-
Excel.CellType.STRING -> {
294+
ExcelCellType.STRING -> {
293295
val dictType = excel.dictType
294296
if (StringUtils.isBlank(dictType)) {
295297
cell.setCellValue(stringValue)
@@ -304,13 +306,13 @@ object ExcelUtils {
304306
cell.setCellValue(DictUtils.getDictLabel(dictType, stringValue))
305307
cell.sheet.addValidationData(validation)
306308
}
307-
Excel.CellType.NUMBER -> {
309+
ExcelCellType.NUMBER -> {
308310
if (StringUtils.isBlank(stringValue)) {
309311
return
310312
}
311313
cell.setCellValue(stringValue.toDouble())
312314
}
313-
Excel.CellType.DATE -> {
315+
ExcelCellType.DATE -> {
314316
if (StringUtils.isBlank(stringValue)) {
315317
return
316318
}

tang-system/src/main/java/com/tang/system/entity/SysUser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import org.hibernate.validator.constraints.Length;
77

88
import com.tang.commons.annotation.poi.Excel;
9-
import com.tang.commons.annotation.poi.Excel.CellType;
10-
import com.tang.commons.annotation.poi.Excel.Type;
119
import com.tang.commons.base.entity.BaseEntity;
10+
import com.tang.commons.enumeration.poi.CellType;
11+
import com.tang.commons.enumeration.poi.Type;
1212

1313
import jakarta.validation.constraints.Email;
1414
import jakarta.validation.constraints.Max;

0 commit comments

Comments
 (0)