easyExcel读excel文件创建表并导入数据
1.添加maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.4</version>
</dependency>
2.创建NoModleDataListener
@Slf4j
public class NoModleDataListener extends AnalysisEventListener<Map<Integer, String>>{
private JdbcTemplate jdbcTemplate;
private static final int BATCH_COUNT = 5000;
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
private int i = 0;
Map<Integer, String> fieldMap = new HashMap<>();
public NoModleDataListener(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
i++;
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
fieldMap = headMap;
log.info("解析到一条头数据:{}", headMap);
StringBuilder sb = new StringBuilder();
sb.append("create table ").append(dataSet.getTableName()).append(" ( ");
for(Integer key: headMap.keySet()){
sb.append(headMap.get(key)).append(" ").append("TEXT,");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(" )");
log.info(sb.toString());
jdbcTemplate.execute(sb.toString());
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
log.info("所有数据解析完成,一共{}条数据!",i);
}
private void saveData() {
log.info("{}条数据,开始存储数据库!", list.size());
StringBuilder insertSql = new StringBuilder();
insertSql.append(" INSERT INTO ");
insertSql.append(dataSet.getTableName());
insertSql.append(" (");
StringBuilder columnSql = new StringBuilder();
StringBuilder paramSql = new StringBuilder();
for(int i = 0 ; i< fieldMap.size() ; i ++){
columnSql.append(",");
columnSql.append(fieldMap.get(i));
paramSql.append(",");
paramSql.append("?");
}
insertSql.append(columnSql.substring(1));
insertSql.append(") VALUES (");
insertSql.append(paramSql.substring(1));
insertSql.append(")");
List<Object[]> listStr = new ArrayList<>();
for(int i = 0 ; i<list.size(); i++){
Map<Integer, String> dataMap = list.get(i);
String[] strings = new String[dataMap.size()];
for(Integer key: dataMap.keySet()){
strings[key] = dataMap.get(key);
}
listStr.add(strings);
}
jdbcTemplate.batchUpdate(insertSql.toString(),listStr);
log.info("存储数据库成功!");
}
}
3.ecxel导入实现方法
@Override
public void addExcelDataSet(MultipartFile file,String datasourceId) throws IOException {
DatabaseInfo databaseInfo = sjztBaseDatasourceDao.getSjztBaseDatasourceById(datasourceId);
JdbcTemplate jdbcTemplate = DataSourceUtils.getJdbcTemplate(
sjztBaseDatasourceService.databaseInfoPwdDecode(databaseInfo));
EasyExcel.read(file.getInputStream(), new NoModleDataListener(jdbcTemplate)).sheet().doRead();
}
4.controller代码
@ApiOperation(value = "excel导入", notes = "excel导入", httpMethod = "POST")
@PostMapping(value = "/addExcel")
public Result addExcel(@ApiParam(value = "excel文件") @RequestParam(value = "file") MultipartFile file,
@ApiParam(value = "数据源ID") @RequestParam(value = "datasourceId") String datasourceId) throws IOException {
sjztDataSetService.addExcelDataSet(file,datasourceId);
return Result.SUCCESS;
}