新增[通用数据导入案例]
This commit is contained in:
parent
73ebd2ceba
commit
bf9860f3bd
@ -294,6 +294,9 @@ public class ExcelKit {
|
|||||||
|
|
||||||
Map map = new HashMap();
|
Map map = new HashMap();
|
||||||
for (int j = 0; j < cellNum && j < fields.length; j++) {
|
for (int j = 0; j < cellNum && j < fields.length; j++) {
|
||||||
|
if (fields[j] == null || "".equals(fields[j])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Cell cell = row.getCell(j);
|
Cell cell = row.getCell(j);
|
||||||
if (cell == null) {
|
if (cell == null) {
|
||||||
map.put(fields[j], "");
|
map.put(fields[j], "");
|
||||||
@ -385,23 +388,6 @@ public class ExcelKit {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
/*Workbook wk;
|
|
||||||
FileInputStream fis = new FileInputStream(file);
|
|
||||||
try {
|
|
||||||
wk = new HSSFWorkbook(fis);//if excel version 2007+ will throws OfficeXmlFileException
|
|
||||||
} catch (OfficeXmlFileException e) {
|
|
||||||
wk = new SXSSFWorkbook(new XSSFWorkbook(fis));
|
|
||||||
} finally {
|
|
||||||
if (fis != null) {
|
|
||||||
try {
|
|
||||||
fis.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
//e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return wk;*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//dev
|
//dev
|
||||||
|
@ -17,11 +17,15 @@ public class ExcelKitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mapExport(){
|
public void mapExport(){
|
||||||
// 数据List<T> T可以是map,也可以是某个Javabean
|
// 数据List<T> T可以是map,也可以是某个Javabean
|
||||||
List list = asList(
|
Map m1 = new HashMap();
|
||||||
Map.of("name", "张三", "age", 12), //jdk9+语法,创建Map并初始化数据
|
m1.put("name", "张三");
|
||||||
Map.of("name", "李四", "age", 11)
|
m1.put("age", 12);
|
||||||
);
|
|
||||||
|
|
||||||
|
Map m2 = new HashMap();
|
||||||
|
m2.put("name", "李四");
|
||||||
|
m2.put("age", 11);
|
||||||
|
|
||||||
|
List list = asList(m1, m2);
|
||||||
// 表头数据 k-v,k:map的数据key,v:表头展示的名称
|
// 表头数据 k-v,k:map的数据key,v:表头展示的名称
|
||||||
LinkedHashMap heads = new LinkedHashMap();
|
LinkedHashMap heads = new LinkedHashMap();
|
||||||
heads.put("name", "姓名");
|
heads.put("name", "姓名");
|
||||||
@ -81,4 +85,74 @@ public class ExcelKitTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------- 通用导入案例 --------------------------------
|
||||||
|
|
||||||
|
static Properties properties = new Properties();
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
// 读取导入配置文件
|
||||||
|
properties.load(new FileReader(new File("test/resource/import.txt")));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建入库语句
|
||||||
|
* @param list 数据list<Map>
|
||||||
|
* @param heads 数据库需要入库的字段
|
||||||
|
* @param table 实体表
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String buildSql(List<Map> list, String[] heads, String table) {
|
||||||
|
StringBuilder bufKs = new StringBuilder();
|
||||||
|
StringBuilder bufVs = new StringBuilder();
|
||||||
|
|
||||||
|
for (String k : heads) {
|
||||||
|
if (!k.isEmpty()) {
|
||||||
|
bufKs.append("`" + k + "`,");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bufKs.deleteCharAt(bufKs.length() - 1);
|
||||||
|
|
||||||
|
list.forEach(m -> {
|
||||||
|
bufVs.append("(");
|
||||||
|
for (String k : heads) {
|
||||||
|
if (!k.isEmpty()) {
|
||||||
|
bufVs.append("'" + m.get(k) + "',");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bufVs.deleteCharAt(bufVs.length() - 1); //去除最后多余的 逗号
|
||||||
|
bufVs.append("),");
|
||||||
|
});
|
||||||
|
bufVs.deleteCharAt(bufVs.length() - 1); //去除最后多余的 逗号
|
||||||
|
return String.format("INSERT INTO %s (%s) VALUES %s;", table, bufKs, bufVs); // table, ks, vs
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用数据导入
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void importTest() {
|
||||||
|
String key = "user"; // 假定使用业务实体表名作为key
|
||||||
|
|
||||||
|
// 获取需要导入的列
|
||||||
|
String cfg = properties.getProperty(key);
|
||||||
|
String[] heads = cfg.split(",");
|
||||||
|
for (int i = 0; i < heads.length; i++) {
|
||||||
|
heads[i] = heads[i].trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过配置读取数据,userData.xlsx为待导入数据
|
||||||
|
List<Map> list = ExcelKit.readExcel(new File("test/resource/userData.xlsx"), heads);
|
||||||
|
list.remove(0); // 去除多余的行首两行
|
||||||
|
list.remove(0);
|
||||||
|
|
||||||
|
System.out.println(list); // 打印出数据看看是否ok
|
||||||
|
|
||||||
|
// 创建入库语句
|
||||||
|
String sql = buildSql(list, heads, key);
|
||||||
|
System.out.println(sql);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
2
test/resource/import.txt
Normal file
2
test/resource/import.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# 假定我们就使用数据库对应的 “列名”作为导入配置,如果excel中间有需要跳过的列 配置中用空号占位
|
||||||
|
user= ,username, , sex,phone,email
|
BIN
test/resource/userData.xlsx
Normal file
BIN
test/resource/userData.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user