前一段时间一直在忙其他的事情,一直没空写,现在有点空闲时间,正好把前二天用了的技术写下来。
需求:在差不多10张表中插入百来条真实数据。
解决方案:打算将数据存放在excal中,读取excal的数据插入到数据库中。如果以后可能还会插入数据到其他表中,打算就写一个接口。2个参数,一个是表的名字,一个是excal的路径。传入之后会自动读取到数据插入到表中。
开始想法:获取到数据之后通过mybatis插入到数据库中,遇到的问题是:开始打算用mybatis中foreach标签循环插入数据,因为只打算写一个接口,map的key不确定,在网上找资料也没有找到动态的key的获取。 放弃该方法,之后仔细想了想,用jdbc可以实现。因为jdbc可以用字符串拼接插入多条数据。不需要知道字段的名字,只要excal的第一行字段和数据库的字段对应就可以插入。
准备阶段:excal表格一份,获取excal数据的 jar包
![ea27b3ac3c72bd5af85fa5dcb03f5f82.png](https://i-blog.csdnimg.cn/blog_migrate/2bb4cee518b98d5321d0dc6482e753d6.jpeg)
获取excal数据需要的 jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
开发环境:idea
public static void main(String[] args) {
String columns[] = null;
Workbook wb =null;
Sheet sheet = null;
Row row = null;
List<Map<String,String>> list = null;
String cellData = null;
String filePath = "C:Users38147Desktopuser_test.xls"; //这里是excal的路径
wb = readExcel(filePath);
if(wb != null){
//用来存放表中数据
list = new ArrayList<>();
//获取第一个sheet
sheet = wb.getSheetAt(0);
//获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
//获取第一行
row = sheet.getRow(0);
//获取最大列数
int colnum = row.getPhysicalNumberOfCells();
for (int i = 0; i<rownum; i++) {
if (i == 0 ) {
row = sheet.getRow(i);
columns = new String[colnum];
if (row != null) {
for (int j = 0; j < colnum; j++) {
columns[j] = String.valueOf(row.getCell(j));
}
}
}else {
Map<String, String> map = new LinkedHashMap<>();
row = sheet.getRow(i);
if (row != null && (!row.equals(""))) {
for (int j = 0; j < colnum; j++) {
cellData = (String) getCellFormatValue(row.getCell(j));
map.put(columns[j], cellData);
}
list.add(map);
} else {
break;
}
}
}
}
for (Map map:list) {
System.out.println(map.keySet() + ""+ map.values());
}
}
//读取excel
public static Workbook readExcel(String filePath){
Workbook wb = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if(".xls".equals(extString)){
return wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return wb = new XSSFWorkbook(is);
}else{
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static Object getCellFormatValue(org.apache.poi.ss.usermodel.Cell cell){
Object cellValue = null;
if(cell!=null){
//判断cell类型
switch(cell.getCellType()){
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:{
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
return sdf.format(date).toString();
}
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:{
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
return sdf.format(date).toString();
}
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}
}
这是从网上找来的CellType类型以及值的对应关系
![37899e21620a9a703fc8add8a5eb9109.png](https://i-blog.csdnimg.cn/blog_migrate/0aee20735edc775b073ff2683b9f9a81.jpeg)
直接运行上面的代码就可以打印出:
![147d6c95f3086b3b9b70cd05771224f5.png](https://i-blog.csdnimg.cn/blog_migrate/f464611a89c5687c415a53987a798df3.jpeg)
读取excal就已经完成了,等下次空余时间在写把数据插入到数据库中,插入数据库是挺简单的,这里有点困难的是上面说到的就写一个接口,表和路径换了也可以插入,还是有点难度的,而且现在打印出来的是double类型的,而我们的需要的类型int类型的。大家可以思考一下,等下次有空的时候继续写。