mirror of https://github.com/alibaba/easyexcel
zhuangjiaju
5 years ago
28 changed files with 377 additions and 160 deletions
@ -0,0 +1,8 @@
|
||||
package com.alibaba.excel; |
||||
|
||||
/** |
||||
* This is actually {@link EasyExcelFactory}, and it's nice to have a short name |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class EasyExcel extends EasyExcelFactory {} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.format.DateTimeFormat; |
||||
import com.alibaba.excel.annotation.format.NumberFormat; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类.这里的排序和excel里面的排序一致 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class ConverterData { |
||||
/** |
||||
* 我自定义 转换器,不管数据库传过来什么 。我给他加上“自定义:” |
||||
*/ |
||||
@ExcelProperty(converter = CustomStringStringConverter.class) |
||||
private String string; |
||||
/** |
||||
* 这里用string 去接日期才能格式化。我想接收年月日格式 |
||||
*/ |
||||
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") |
||||
private String date; |
||||
/** |
||||
* 我想接收百分比的数字 |
||||
*/ |
||||
@NumberFormat("#.##%") |
||||
private String doubleData; |
||||
} |
@ -0,0 +1,49 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 模板的读取类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ConverterDataListener extends AnalysisEventListener<ConverterData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<ConverterData> list = new ArrayList<ConverterData>(); |
||||
|
||||
@Override |
||||
public void invoke(ConverterData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
list.add(data); |
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
list.clear(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
private void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
/** |
||||
* String and string converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class CustomStringStringConverter implements Converter<String> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return String.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
/** |
||||
* 这里读的时候会调用 |
||||
* |
||||
* @param cellData |
||||
* NotNull |
||||
* @param contentProperty |
||||
* Nullable |
||||
* @param globalConfiguration |
||||
* NotNull |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return "自定义:" + cellData.getStringValue(); |
||||
} |
||||
|
||||
/** |
||||
* 这里是写的时候会调用 不用管 |
||||
* |
||||
* @param value |
||||
* NotNull |
||||
* @param contentProperty |
||||
* Nullable |
||||
* @param globalConfiguration |
||||
* NotNull |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return new CellData(value); |
||||
} |
||||
|
||||
} |
Binary file not shown.
Loading…
Reference in new issue