Skip to content

Commit d696a25

Browse files
authored
doc: Add JavaDoc for read cases (#301)
1 parent 81cbaab commit d696a25

13 files changed

Lines changed: 202 additions & 187 deletions

fastexcel-test/src/test/java/cn/idev/excel/test/demo/read/ConverterData.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88
import lombok.Setter;
99

1010
/**
11-
* 基础数据类.这里的排序和excel里面的排序一致
11+
* Basic data class. The order here is consistent with the order in the Excel file.
1212
*
1313
* @author Jiaju Zhuang
1414
**/
1515
@Getter
1616
@Setter
1717
@EqualsAndHashCode
1818
public class ConverterData {
19-
19+
2020
/**
21-
* 我自定义 转换器,不管数据库传过来什么 。我给他加上“自定义:”
21+
* I use a custom converter. No matter what is passed from the database, I prepend "Custom:".
2222
*/
2323
@ExcelProperty(converter = CustomStringStringConverter.class)
2424
private String string;
25-
25+
2626
/**
27-
* 这里用string 去接日期才能格式化。我想接收年月日格式
27+
* I use a string to receive the date so that it can be formatted. I want to receive the date in the format of yyyy-MM-dd HH:mm:ss.
2828
*/
29-
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
29+
@DateTimeFormat("yyyy-MM-dd HH:mm:ss")
3030
private String date;
31-
31+
3232
/**
33-
* 我想接收百分比的数字
33+
* I want to receive a number in percentage format.
3434
*/
3535
@NumberFormat("#.##%")
3636
private String doubleData;

fastexcel-test/src/test/java/cn/idev/excel/test/demo/read/ConverterDataListener.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,42 @@
99
import java.util.List;
1010

1111
/**
12-
* 模板的读取类
12+
* Template data reading class
1313
*
1414
* @author Jiaju Zhuang
1515
*/
1616
@Slf4j
1717
public class ConverterDataListener implements ReadListener<ConverterData> {
18-
18+
1919
/**
20-
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
20+
* Save to the database every 5 records. In actual use, you might use 100 records,
21+
* then clear the list to facilitate memory recycling.
2122
*/
2223
private static final int BATCH_COUNT = 5;
23-
24+
2425
private List<ConverterData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
25-
26+
2627
@Override
2728
public void invoke(ConverterData data, AnalysisContext context) {
28-
log.info("解析到一条数据:{}", JSON.toJSONString(data));
29+
log.info("Parsed a piece of data: {}", JSON.toJSONString(data));
2930
cachedDataList.add(data);
3031
if (cachedDataList.size() >= BATCH_COUNT) {
3132
saveData();
3233
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
3334
}
3435
}
35-
36+
3637
@Override
3738
public void doAfterAllAnalysed(AnalysisContext context) {
3839
saveData();
39-
log.info("所有数据解析完成!");
40+
log.info("All data has been parsed and processed!");
4041
}
41-
42+
4243
/**
43-
* 加上存储数据库
44+
* Simulate saving data to the database
4445
*/
4546
private void saveData() {
46-
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
47-
log.info("存储数据库成功!");
47+
log.info("Saving {} records to the database!", cachedDataList.size());
48+
log.info("Data saved to the database successfully!");
4849
}
4950
}

fastexcel-test/src/test/java/cn/idev/excel/test/demo/read/CustomStringStringConverter.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,38 @@
1212
* @author Jiaju Zhuang
1313
*/
1414
public class CustomStringStringConverter implements Converter<String> {
15-
15+
1616
@Override
1717
public Class<?> supportJavaTypeKey() {
1818
return String.class;
1919
}
20-
20+
2121
@Override
2222
public CellDataTypeEnum supportExcelTypeKey() {
2323
return CellDataTypeEnum.STRING;
2424
}
25-
25+
2626
/**
27-
* 这里读的时候会调用
27+
* This method is called when reading data from Excel.
2828
*
29-
* @param context
30-
* @return
29+
* @param context The context containing the cell data read from Excel.
30+
* @return The converted Java data.
3131
*/
3232
@Override
3333
public String convertToJavaData(ReadConverterContext<?> context) {
34-
return "自定义:" + context.getReadCellData().getStringValue();
34+
return "Custom:" + context.getReadCellData().getStringValue();
3535
}
36-
36+
3737
/**
38-
* 这里是写的时候会调用 不用管
38+
* This method is called when writing data to Excel.
39+
* (This is not relevant in this context and can be ignored.)
3940
*
40-
* @return
41+
* @param context The context containing the Java data to be written.
42+
* @return The converted Excel data.
4143
*/
4244
@Override
4345
public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
4446
return new WriteCellData<>(context.getValue());
4547
}
46-
48+
4749
}

fastexcel-test/src/test/java/cn/idev/excel/test/demo/read/DemoExceptionListener.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,70 @@
1212
import java.util.Map;
1313

1414
/**
15-
* 读取转换异常
15+
* Read and convert exceptions.
1616
*
1717
* @author Jiaju Zhuang
1818
*/
1919
@Slf4j
2020
public class DemoExceptionListener implements ReadListener<ExceptionDemoData> {
21-
21+
2222
/**
23-
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
23+
* Save to the database every 5 records. In actual use, it can be set to 100 records, and then clear the list to facilitate memory recycling.
2424
*/
2525
private static final int BATCH_COUNT = 5;
26-
26+
2727
private List<ExceptionDemoData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
28-
28+
2929
/**
30-
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
30+
* This interface will be called in case of conversion exceptions or other exceptions. If an exception is thrown here, reading will be stopped. If no exception is thrown, the next line will continue to be read.
3131
*
32-
* @param exception
33-
* @param context
34-
* @throws Exception
32+
* @param exception The exception that occurred.
33+
* @param context The analysis context.
34+
* @throws Exception If an exception needs to be propagated.
3535
*/
3636
@Override
3737
public void onException(Exception exception, AnalysisContext context) {
38-
log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
39-
// 如果是某一个单元格的转换异常 能获取到具体行号
40-
// 如果要获取头的信息 配合invokeHeadMap使用
38+
log.error("Parsing failed, but continue parsing the next line: {}", exception.getMessage());
39+
// If it is a cell conversion exception, the specific row number can be obtained.
40+
// If the header information is needed, use it in conjunction with invokeHeadMap.
4141
if (exception instanceof ExcelDataConvertException) {
4242
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
43-
log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(),
43+
log.error("Parsing exception in row {}, column {}, data: {}", excelDataConvertException.getRowIndex(),
4444
excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
4545
}
4646
}
47-
47+
4848
/**
49-
* 这里会一行行的返回头
49+
* This method will return the header row line by line.
5050
*
51-
* @param headMap
52-
* @param context
51+
* @param headMap The header map containing column index and cell data.
52+
* @param context The analysis context.
5353
*/
5454
@Override
5555
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
56-
log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
56+
log.info("Parsed a header row: {}", JSON.toJSONString(headMap));
5757
}
58-
58+
5959
@Override
6060
public void invoke(ExceptionDemoData data, AnalysisContext context) {
61-
log.info("解析到一条数据:{}", JSON.toJSONString(data));
61+
log.info("Parsed a data row: {}", JSON.toJSONString(data));
6262
if (cachedDataList.size() >= BATCH_COUNT) {
6363
saveData();
6464
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
6565
}
6666
}
67-
67+
6868
@Override
6969
public void doAfterAllAnalysed(AnalysisContext context) {
7070
saveData();
71-
log.info("所有数据解析完成!");
71+
log.info("All data parsing completed!");
7272
}
73-
73+
7474
/**
75-
* 加上存储数据库
75+
* Save data to the database.
7676
*/
7777
private void saveData() {
78-
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
79-
log.info("存储数据库成功!");
78+
log.info("{} records, starting to save to the database!", cachedDataList.size());
79+
log.info("Data saved to the database successfully!");
8080
}
8181
}

fastexcel-test/src/test/java/cn/idev/excel/test/demo/read/DemoExtraListener.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,47 @@
88
import org.junit.jupiter.api.Assertions;
99

1010
/**
11-
* 读取单元格的批注
11+
* Listener to read cell comments, hyperlinks, and merged cells.
1212
*
1313
* @author Jiaju Zhuang
1414
**/
1515
@Slf4j
1616
public class DemoExtraListener implements ReadListener<DemoExtraData> {
17-
17+
1818
@Override
1919
public void invoke(DemoExtraData data, AnalysisContext context) {
2020
}
21-
21+
2222
@Override
2323
public void doAfterAllAnalysed(AnalysisContext context) {
2424
}
25-
25+
2626
@Override
2727
public void extra(CellExtra extra, AnalysisContext context) {
28-
log.info("读取到了一条额外信息:{}", JSON.toJSONString(extra));
28+
log.info("Read an extra piece of information: {}", JSON.toJSONString(extra));
2929
switch (extra.getType()) {
3030
case COMMENT:
31-
log.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(),
31+
log.info("The extra information is a comment, at rowIndex:{}, columnIndex:{}, content:{}", extra.getRowIndex(),
3232
extra.getColumnIndex(), extra.getText());
3333
break;
3434
case HYPERLINK:
3535
if ("Sheet1!A1".equals(extra.getText())) {
36-
log.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(),
36+
log.info("The extra information is a hyperlink, at rowIndex:{}, columnIndex:{}, content:{}", extra.getRowIndex(),
3737
extra.getColumnIndex(), extra.getText());
3838
} else if ("Sheet2!A1".equals(extra.getText())) {
39-
log.info(
40-
"额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{},"
41-
+ "内容是:{}", extra.getFirstRowIndex(), extra.getFirstColumnIndex(),
39+
log.info("The extra information is a hyperlink, covering a range, firstRowIndex:{}, firstColumnIndex:{}, " +
40+
"lastRowIndex:{}, lastColumnIndex:{}, content:{}", extra.getFirstRowIndex(), extra.getFirstColumnIndex(),
4241
extra.getLastRowIndex(), extra.getLastColumnIndex(), extra.getText());
4342
} else {
4443
Assertions.fail("Unknown hyperlink!");
4544
}
4645
break;
4746
case MERGE:
4847
log.info(
49-
"额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}",
50-
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
51-
extra.getLastColumnIndex());
48+
"The extra information is a merged cell, covering a range, firstRowIndex:{}, firstColumnIndex:{}, " +
49+
"lastRowIndex:{}, lastColumnIndex:{}",
50+
extra.getFirstRowIndex(), extra.getFirstColumnIndex(),
51+
extra.getLastRowIndex(), extra.getLastColumnIndex());
5252
break;
5353
default:
5454
}

0 commit comments

Comments
 (0)