Skip to content

Commit 7f0c78a

Browse files
committed
v2.1.0
1 parent 968315d commit 7f0c78a

File tree

12 files changed

+133
-75
lines changed

12 files changed

+133
-75
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
// 读取数据转换器 Student2ExpelConverter
2828
@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)
2929
private boolean expel;
30-
3130
```
3231

3332
## 三. 读取Excel快速实现
@@ -160,7 +159,7 @@ Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, cl
160159
@Test
161160
public void testObject2Excel() throws Exception {
162161
163-
String tempPath = "D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\normal_template.xlsx";
162+
String tempPath = "/normal_template.xlsx";
164163
List<Student1> list = new ArrayList<>();
165164
list.add(new Student1("1010001", "盖伦", "六年级三班"));
166165
list.add(new Student1("1010002", "古尔丹", "一年级三班"));
@@ -223,7 +222,7 @@ Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, cl
223222
add(new Student1("1010003", "蒙多", "六年级一班"));
224223
}});
225224
226-
ExcelUtils.getInstance().exportObject2Excel("D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\map_template.xlsx",
225+
ExcelUtils.getInstance().exportObject2Excel("/map_template.xlsx",
227226
0, classes, data, Student1.class, false, "D:/C.xlsx");
228227
}
229228
```

src/main/java/com/github/crab2died/ExcelUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public static ExcelUtils getInstance() {
6363
/* *) limitLine => 最大读取行数(默认表尾) */
6464
/* *) sheetIndex => Sheet索引(默认0) */
6565

66-
public <T> List<T> readExcel2Objects(String excelPath, Class<T> clazz, int offsetLine, int limitLine, int
67-
sheetIndex) throws Exception {
66+
public <T> List<T> readExcel2Objects(String excelPath, Class<T> clazz, int offsetLine, int limitLine,
67+
int sheetIndex) throws Exception {
6868
Workbook workbook = WorkbookFactory.create(new File(excelPath));
6969
return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex);
7070
}
@@ -100,9 +100,8 @@ public <T> List<T> readExcel2Objects(InputStream is, Class<T> clazz)
100100
return readExcel2Objects(is, clazz, 0, Integer.MAX_VALUE, 0);
101101
}
102102

103-
private <T> List<T> readExcel2ObjectsHandler(Workbook workbook, Class<T> clazz,
104-
int offsetLine, int limitLine,
105-
int sheetIndex) throws Exception {
103+
private <T> List<T> readExcel2ObjectsHandler(Workbook workbook, Class<T> clazz, int offsetLine,
104+
int limitLine, int sheetIndex) throws Exception {
106105

107106
Sheet sheet = workbook.getSheetAt(sheetIndex);
108107
Row row = sheet.getRow(offsetLine);

src/main/java/com/github/crab2died/annotation/ExcelField.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,34 @@
3030
@Retention(RetentionPolicy.RUNTIME)
3131
public @interface ExcelField {
3232

33-
/*
33+
/**
3434
* 属性的标题名称
35+
* @return 表头名
3536
*/
3637
String title();
3738

38-
/*
39+
/**
3940
* 写数据转换器
41+
*
42+
* @see WriteConvertible
43+
* @return 写入Excel数据转换器
4044
*/
4145
Class<? extends WriteConvertible> writeConverter()
4246
default DefaultConvertible.class;
4347

44-
/*
48+
/**
4549
* 读数据转换器
50+
*
51+
* @see ReadConvertible
52+
* @return 读取Excel数据转换器
4653
*/
4754
Class<? extends ReadConvertible> readConverter()
4855
default DefaultConvertible.class;
4956

50-
/*
57+
/**
5158
* 在excel的顺序
59+
*
60+
* @return 列表顺序
5261
*/
5362
int order() default 9999;
5463
}

src/main/java/com/github/crab2died/converter/DefaultConvertible.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.github.crab2died.converter;
1919

2020
/**
21-
* 默认转换器
21+
* 默认转换器, 实现了{@link WriteConvertible} 与 {@link ReadConvertible}接口
2222
*/
2323
public class DefaultConvertible implements WriteConvertible, ReadConvertible {
2424

src/main/java/com/github/crab2died/converter/ReadConvertible.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@
2222
*/
2323
public interface ReadConvertible {
2424

25+
/**
26+
* 读取Excel列内容转换
27+
*
28+
* @see com.github.crab2died.annotation.ExcelField#readConverter()
29+
* @param object 待转换数据
30+
* @return 转换完成的结果
31+
*/
2532
Object execRead(String object);
2633
}

src/main/java/com/github/crab2died/converter/WriteConvertible.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@
2222
*/
2323
public interface WriteConvertible {
2424

25+
/**
26+
* 写入Excel列内容转换
27+
*
28+
* @see com.github.crab2died.annotation.ExcelField#writeConverter()
29+
* @param object 待转换数据
30+
* @return 转换完成的结果
31+
*/
2532
Object execWrite(Object object);
2633
}

src/main/java/com/github/crab2died/handler/ExcelTemplate.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.github.crab2died.handler;
1919

20+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
2021
import org.apache.poi.ss.usermodel.*;
2122

2223
import java.io.*;
@@ -101,21 +102,24 @@ public class ExcelTemplate {
101102
private ExcelTemplate() {
102103
}
103104

104-
public static ExcelTemplate getInstance(String templatePath, int sheetIndex) {
105+
public static ExcelTemplate getInstance(String templatePath, int sheetIndex) throws Exception {
105106
ExcelTemplate template = new ExcelTemplate();
106107
template.sheetIndex = sheetIndex;
107-
try {
108-
template.loadTemplate(templatePath);
109-
} catch (Exception e) {
110-
e.printStackTrace();
111-
}
108+
template.loadTemplate(templatePath);
112109
return template;
113110
}
114111

115112
/*-----------------------------------初始化模板开始-----------------------------------*/
116113

117-
private void loadTemplate(String templatePath) throws Exception {
118-
this.workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(templatePath));
114+
private void loadTemplate(String templatePath) throws IOException, InvalidFormatException {
115+
116+
try {
117+
// 读取模板文件
118+
this.workbook = WorkbookFactory.create(new File(templatePath));
119+
} catch (IOException | InvalidFormatException e) {
120+
// 读取模板相对文件
121+
this.workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(templatePath));
122+
}
119123
this.sheet = this.workbook.getSheetAt(this.sheetIndex);
120124
initModuleConfig();
121125
this.currentRowIndex = this.initRowIndex;
@@ -336,12 +340,12 @@ private void setCellStyle(Cell cell, String styleKey) {
336340
/**
337341
* 将文件写到相应的路径下
338342
*
339-
* @param filepath 输出文件路径
343+
* @param filePath 输出文件路径
340344
*/
341-
public void write2File(String filepath) {
345+
public void write2File(String filePath) {
342346

343347
try {
344-
try (FileOutputStream fos = new FileOutputStream(filepath)) {
348+
try (FileOutputStream fos = new FileOutputStream(filePath)) {
345349
try {
346350
this.workbook.write(fos);
347351
} catch (IOException e) {

src/main/java/com/github/crab2died/handler/HandlerConstant.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
/**
2222
* <p>Excel模板自定义属性,不区分大小写</p>
2323
*/
24-
public class HandlerConstant {
24+
class HandlerConstant {
2525

2626
// 数据插入起始坐标点
27-
static protected final String DATA_INIT_INDEX = "$data_index";
27+
static final String DATA_INIT_INDEX = "$data_index";
2828

2929
// 默认样式
30-
static protected final String DEFAULT_STYLE = "$default_style";
30+
static final String DEFAULT_STYLE = "$default_style";
3131

3232
// 当前标记行样式
33-
static protected final String APPOINT_LINE_STYLE = "$appoint_line_style";
33+
static final String APPOINT_LINE_STYLE = "$appoint_line_style";
3434

3535
// 单数行样式
36-
static protected final String SINGLE_LINE_STYLE = "$single_line_style";
36+
static final String SINGLE_LINE_STYLE = "$single_line_style";
3737

3838
// 双数行样式
39-
static protected final String DOUBLE_LINE_STYLE = "$double_line_style";
39+
static final String DOUBLE_LINE_STYLE = "$double_line_style";
4040

4141
// 序号列坐标点
42-
static protected final String SERIAL_NUMBER = "$serial_number";
42+
static final String SERIAL_NUMBER = "$serial_number";
4343

4444
}

src/main/java/com/github/crab2died/utils/MethodType.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/com/github/crab2died/utils/RegularUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ static public String match(String pattern, String reg) {
9090

9191
String match = null;
9292
try {
93-
List<String> matchs = match(pattern, reg, 0);
94-
if (null != matchs && matchs.size() > 0) {
95-
match = matchs.get(0);
93+
List<String> matches = match(pattern, reg, 0);
94+
if (null != matches && matches.size() > 0) {
95+
match = matches.get(0);
9696
}
9797
} catch (IllegalGroupIndexException e) {
9898
e.printStackTrace();

0 commit comments

Comments
 (0)