|
|
|
@ -357,24 +357,23 @@ public class ImportUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:导入数据
|
|
|
|
|
*
|
|
|
|
|
* @param table_name
|
|
|
|
|
* @param filePath
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws ParseException
|
|
|
|
|
*/
|
|
|
|
|
public static void importData(String table_name, String filePath) throws IOException, ParseException {
|
|
|
|
|
InputStream is = new FileInputStream(filePath);
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
public static void importData(String upload_excel_filename, XSSFWorkbook wb, int sheetIdx, String bureau_id, String person_id) throws IOException, ParseException {
|
|
|
|
|
//读取sheet页
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
//通过表名获取到它的读取起始行,终止列
|
|
|
|
|
String sql = "select * from t_importexcel_config where table_name=?";
|
|
|
|
|
Record record = Db.findFirst(sql, table_name);
|
|
|
|
|
String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?";
|
|
|
|
|
Record record = Db.findFirst(sql, upload_excel_filename, sheetIdx);
|
|
|
|
|
String table_name = record.getStr("table_name");
|
|
|
|
|
|
|
|
|
|
int data_start_row = record.getInt("data_start_row");
|
|
|
|
|
int column_num = record.getInt("column_num");
|
|
|
|
|
|
|
|
|
|
//获取字段与EXCEL列的映射信息
|
|
|
|
|
sql = "select * from t_importexcel_mapping where table_name=?";
|
|
|
|
|
List<Record> list = Db.find(sql, table_name);
|
|
|
|
|
sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
|
|
|
|
|
List<Record> list = Db.find(sql, upload_excel_filename, sheetIdx);
|
|
|
|
|
Map<Integer, Record> _map = new HashMap<>();
|
|
|
|
|
for (Record r : list) {
|
|
|
|
|
int excel_column_idx = r.getInt("excel_column_idx");
|
|
|
|
@ -387,25 +386,27 @@ public class ImportUtil {
|
|
|
|
|
}
|
|
|
|
|
//开始读取数据
|
|
|
|
|
List<Record> writeList = new ArrayList<>();
|
|
|
|
|
for (int i = data_start_row - 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
for (int i = data_start_row; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
//获得行
|
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
|
XSSFRow row = sheet.getRow(i);
|
|
|
|
|
|
|
|
|
|
//遍历列
|
|
|
|
|
if (row != null) {
|
|
|
|
|
Record writeRecord = new Record();
|
|
|
|
|
writeRecord.set("bureau_id", bureau_id);
|
|
|
|
|
writeRecord.set("person_id", person_id);
|
|
|
|
|
for (int j = 0; j < column_num; j++) {
|
|
|
|
|
Cell cell = row.getCell(j);
|
|
|
|
|
CellBean bean = getCellBean(cell);
|
|
|
|
|
String colType = _map.get(j + 1).getStr("column_type");
|
|
|
|
|
String colName = _map.get(j + 1).getStr("column_name");
|
|
|
|
|
if (colType.equals("int"))
|
|
|
|
|
writeRecord.set(colName, Integer.parseInt(bean.getValue()));
|
|
|
|
|
else if (colType.equals("varchar"))
|
|
|
|
|
writeRecord.set(colName, bean.getValue());
|
|
|
|
|
else if (colType.equals("float"))
|
|
|
|
|
writeRecord.set(colName, Float.parseFloat(bean.getValue().toString()));
|
|
|
|
|
else if (colType.equals("date")) {
|
|
|
|
|
String dateString = bean.getValue();
|
|
|
|
|
XSSFCell cell = row.getCell(j);
|
|
|
|
|
String colType = _map.get(j).getStr("column_type");
|
|
|
|
|
String colName = _map.get(j).getStr("column_name");
|
|
|
|
|
if (colType.equals("Integer"))
|
|
|
|
|
writeRecord.set(colName, (int) Double.parseDouble(getCellValue(cell).toString()));
|
|
|
|
|
else if (colType.equals("String"))
|
|
|
|
|
writeRecord.set(colName, getCellValue(cell).toString());
|
|
|
|
|
else if (colType.equals("Double"))
|
|
|
|
|
writeRecord.set(colName, Double.parseDouble(getCellValue(cell).toString()));
|
|
|
|
|
else if (colType.equals("Date")) {
|
|
|
|
|
String dateString = getCellValue(cell).toString();
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
Date date = dateFormat.parse(dateString);
|
|
|
|
|
writeRecord.set(colName, date);
|
|
|
|
@ -414,10 +415,9 @@ public class ImportUtil {
|
|
|
|
|
writeList.add(writeRecord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("写入数据" + writeList.size() + "条!");
|
|
|
|
|
// 写入数据
|
|
|
|
|
Db.batchSave(table_name, writeList, 100);
|
|
|
|
|
//关闭excel
|
|
|
|
|
wb.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -611,4 +611,51 @@ public class ImportUtil {
|
|
|
|
|
return cellValueString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断字符串是不是能转换为整数
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isInteger(String str) {
|
|
|
|
|
try {
|
|
|
|
|
double doubleValue = Double.parseDouble(str);
|
|
|
|
|
int intValue = (int) doubleValue;
|
|
|
|
|
return true;
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断字符串是不是能转换为浮点数
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isDouble(String str) {
|
|
|
|
|
try {
|
|
|
|
|
Double.parseDouble(str);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断字符串是不是能转换为日期
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isDate(String str) {
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
try {
|
|
|
|
|
sdf.parse(str);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|