excel导入工具java
整个项目的代码结构以下git
\---excelExport # 导出工具包
| AsyncExportExcel.java #多线程导出
| ExcelImport.java # 导出工具类
|
+---data
| BaseParam.java # 基础导出参数类
|
+---dataConversion
| DataExportConversion.java # 属性导出转换接口
|
+---defaultDataHandle # 默认的数据处理
| AbstractDataHandler.java
| BooleanDataHandler.java
| DataHandlerFactory.java
| DateDataHandler.java
| StringDataHandler.java
|
\---style # 默认的样式
AbstractCellStyle.java
DefaultDataCellStyle.java
DefaultTitleCellStyle.java
com
\---utils
+---demo # 案例相关
| | ExcelImportApplication.java # springboot启动类
| |
| +---bean
| | DemoBean.java # 测试bean
| |
| +---controller
| | ExcelImportController.java # 测试从web导入excel和从文件导入excel
| |
| \---importParam
| | DemoImportParam.java # 导入参数
| |
| \---dataConversion
| HobbyConversion.java # 爱好属性导入转换类
|
\---excelImport # 导入工具包
| ExcelImport.java # 导入工具类
|
+---data
| CellParam.java # 导入列参数类
|
\---dataConversion
| DataImportConversion.java # 属性导入转换接口
|
\---impl
DateConversion.java # 日期属性导入转换接口
MapConversion.java # 键值对属性导入转换接口
复制代码
简单的来讲excel导入能够分为几步github
首先实例化ExcelImport工具类,我这里提供了一个构造函数web
public ExcelImport(Class<T> clazz, List<CellParam> cellParams) 复制代码
参数 | 含义 |
---|---|
clazz | Class对象(须要转换为Bean的Class对象) |
cellParams | CellParam的list列表(每一列对应的字段及数据转换类) |
实例化ExcelImport工具类以后,须要调用importExcel方法,方法定义以下spring
public List<T> importExcel(InputStream is)
复制代码
只须要传入InputStream便可。springboot
CellParam类代码以下bash
public class CellParam {
private String fieldName;
private DataImportConversion conversion;
//Set Get Constructor
}
复制代码
能够看到CellParam类有两个属性多线程
private String fieldName;
private DataImportConversion conversion;
复制代码
参数 | 含义 |
---|---|
fieldName | 列对应Bean的属性 |
conversion | 数据转换类 |
public interface DataImportConversion<T> {
T transferData(Object data);
}
复制代码
我这里默认提供了两种数据转换,一个是键值对,另外一个是日期app
键值对数据转换类是为了将一些通用数据转换而提供的。
例如:男女、是否和一些不一样名称对应的不一样数字(正常-->0,异常-->1,其余-->2)ide
使用者能够经过传入的map的泛型决定返回值的类型。
public class MapConversion<K,V> implements DataImportConversion<V> {
private Map<K,V> map ;
private V defaultReturnValue;
public MapConversion(Map<K, V> map) {
this(map,null);
}
public MapConversion(Map<K, V> map,V defaultReturnValue) {
this.map = map;
this.defaultReturnValue = defaultReturnValue;
}
@Override
public V transferData(Object data) {
if (map == null) return null;
//若是data为null且map的null对应的值不为null,则直接返回map中null对应的值
if (data == null && map.get(null) != null){
return map.get(null);
}
//循环查找对应的key
for (Map.Entry<K,V> entry:map.entrySet()){
if (entry.getKey() != null && entry.getKey().equals(data)){
return entry.getValue();
}
}
//若是map里面找不到对应的数据,则返回defaultReturnValue
return defaultReturnValue;
}
}
复制代码
提供日期转换功能,经过传入的日期转换格式进行转换。
public class DateConversion implements DataImportConversion<Date> {
private SimpleDateFormat format;
public DateConversion(String pattern) {
this.format = new SimpleDateFormat(pattern);
}
@Override
public Date transferData(Object data) {
try {
return format.parse(data.toString());
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}
复制代码
原始数据以下
姓名 | 性别 | 出生日期 | 爱好 |
---|---|---|---|
尘心 | 女 | 2018-08-08 14:59:11 | 舞刀,弄枪 |
千月 | 男 | 2018-08-08 14:59:11 | 唱歌,跳舞 |
须要转换为实体bean的列表,以下
DemoBean{name='尘心', sex=0, birthday=Wed Aug 08 14:13:45 CST 2018, hobbies=[舞刀, 弄枪]}
DemoBean{name='千月', sex=1, birthday=Wed Aug 08 14:13:45 CST 2018, hobbies=[唱歌, 跳舞]}
复制代码
实体类以下
public class DemoBean {
//姓名
private String name;
//性别,0->女,1->男
private Integer sex;
//出生日期
private Date birthday;
//爱好
private List<String> hobbies;
//Set Get
}
复制代码
能够看到有三个属性须要转换,分别是性别、日期和爱好。性别和日期的数据转换能够使用默认的数据转换。爱好须要将字符串根据,
分割并转换为list列表数据,下面是爱好的数据转换,
public class HobbyConversion implements DataImportConversion<List<String>> {
@Override
public List<String> transferData(Object data) {
if (data == null) return null;
//根据,分割字符串
String hobbyStr = data.toString();
String[] hobbyArray = hobbyStr.split(",");
//转换成list
List<String> hobbies = Arrays.asList(hobbyArray);
return hobbies;
}
}
复制代码
数据转换类写好了以后,开始编写导入参数类,代码以下
public class DemoImportParam {
public static List<CellParam> getCellParams(){
Map<String,Integer> sexMap = new HashMap<>();
sexMap.put("女",0);
sexMap.put("男",1);
List<CellParam> cellParams = new ArrayList<>();
cellParams.add(new CellParam("name"));
cellParams.add(new CellParam("sex", new MapConversion(sexMap)));
cellParams.add(new CellParam("birthday", new DateConversion("yyyy-MM-dd HH:mm:ss")));
cellParams.add(new CellParam("hobbies", new HobbyConversion()));
return cellParams;
}
}
复制代码
在DemoImportParam类中能够看到一个静态方法getCellParams,返回List。方法内部先定义了一个mao对象,存放性别字符串对应的数字,而后就是List的定义。
能够看到总共有4个列参数
接下来有两种数据导出方式,一种是url访问弹出下载excel文件,另一种是导出excel到文件
@ResponseBody
@PostMapping("/import")
public List<DemoBean> importByWeb(MultipartFile file) throws Exception {
ExcelImport excelImport = new ExcelImport(DemoBean.class, DemoImportParam.getCellParams());
List<DemoBean> list = excelImport.importExcel(file.getInputStream());
return list;
}
复制代码
@Test
public void importByFile() throws Exception {
File file = new File("F:\\导出demo.xlsx");
FileInputStream inputStream = new FileInputStream(file);
//导入转换
ExcelImport excelImport = new ExcelImport(DemoBean.class, DemoImportParam.getCellParams());
List<DemoBean> list = excelImport.importExcel(inputStream);
//输出
for (DemoBean bean:list){
System.out.println(bean);
}
}
复制代码
项目位置:github.com/rainbowda/u…,有须要的能够看看