最近在项目中使用到了大学专业,需要将专业数据填充到数据库中,网上找数据库资源无果,就想自己生成,在此将过程记录下来。
1.寻找专业文件
在网上随便找了最新的专业表,将数据复制到excel文件中,删除多余的数据
2.编写工具类
编写工具类,读取excel文件,并将数据转为数据库需要的数据,然后转出sql文件。写法上没有优化,只是单纯的实现功能。
package com.wxkc.util;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class ExcelUtil {
public static void main(String[] args) {
try {
test("C:\\Users\\Administrator\\Desktop\\专业.xls","C:\\Users\\Administrator\\Desktop\\profession.sql");
}catch (Exception e){
e.printStackTrace();
}
}
public static void test(String excelPath,String sqlPath) throws Exception{
List<List<String>> list= readExcel(excelPath);
int length=list.size();
List<HashMap<String,Object>> result=new ArrayList<>();
int index=1;
for(int i=0;i<length;i++){
//获取行数据
List<String> dataList=list.get(i);
//获取行中每列数据
int pid=0,ppid=0;
/**门类开始*/
String name1=dataList.get(1);
//判断是否有门类
List<HashMap<String,Object>> lis = result.stream().filter(it -> it.get("name").equals(name1)).collect(Collectors.toList());
if(lis.size()==0){
HashMap<String,Object> map=new HashMap<>();
map.put("name",name1);
map.put("id",index);
map.put("p_id",0);
result.add(map);
pid=index;
index++;
}else{
pid=Integer.parseInt(lis.get(0).get("id").toString());
}
/**门类结束*/
/**专业类开始*/
String name2=dataList.get(2);
//判断是否有专业类
List<HashMap<String,Object>> lis2 = result.stream().filter(it -> it.get("name").equals(name2)).collect(Collectors.toList());
if(lis2.size()==0){
HashMap<String,Object> map=new HashMap<>();
map.put("name",name2);
map.put("id",index);
map.put("p_id",pid);
map.put("stair_id",pid);
result.add(map);
ppid=index;
index++;
}else{
ppid=Integer.parseInt(lis2.get(0).get("id").toString());
}
/**专业类结束*/
/**专业开始*/
String name3=dataList.get(3);
HashMap<String,Object> map=new HashMap<>();
map.put("name",name3);
map.put("id",index);
map.put("p_id",ppid);
map.put("stair_id",pid+","+ppid);
result.add(map);
index++;
/**专业结束*/
}
//将数据导出到sql文件
String sql = "INSERT INTO profession(id, name, add_time,p_id,stair_id,is_del) VALUES (%s, '%s', '%s', %s, '%s', '0');";
FileWriter fw = new FileWriter(new File(sqlPath));
BufferedWriter bw=new BufferedWriter(fw);
int num=result.size();
String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
for(int i=0;i<num;i++){
HashMap<String,Object> map=result.get(i);
//替换内容
String sqlResult = String.format(sql, map.get("id"), map.get("name"), time, map.get("p_id"), map.get("stair_id"));
bw.write(sqlResult+"\n");
}
bw.close();
fw.close();
}
/**
* @Description: 读取excel
*/
public static List<List<String>> readExcel(String filePath) throws IOException, InvalidFormatException {
List<List<String>> result=new ArrayList<>();
if(filePath!=null){
Workbook workbook= WorkbookFactory.create(new File(filePath));
//获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
//获取表的总行数
int num = sheet.getLastRowNum();
//总列数
int col = sheet.getRow(0).getLastCellNum();
//遍历excel每一行
for (int i = 1; i <= num; i++) {
Row row = sheet.getRow(i);
if(row!=null&&!isNullRow(row,col)){
List<String> data=new ArrayList<>();
for (int j = 0; j < col; j++) {
Cell cell=row.getCell(j);
if(cell!=null){
data.add(cell.getStringCellValue().trim());
}else{
data.add("");
}
}
result.add(data);
}
}
workbook.close();
}
return result;
}
/**
* @Description: 判断当前行是否空行
*/
public static boolean isNullRow(Row row,int col){
int num=0;
for (int i = 0; i < col ; i++) {
Cell cell = row.getCell(i);
String content=null;
if(cell!=null){
cell.setCellType(CellType.STRING);
content=cell.getStringCellValue();
}
if(content==null||"".equals(content)){//数据为空
num++;
}
}
return num==col;
}
}
主要使用的jar包