|
|
|
@ -85,7 +85,7 @@ public class ExcelUtil {
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static List<Map.Entry<Integer, Integer>> checkYiZhi(String f1, String f2, int sheetIdx) throws IOException {
|
|
|
|
|
Record record = getSheetConfig(FileUtil.getName(f1), sheetIdx);
|
|
|
|
|
Record record = ImportExcelKit.getSheetConfig(FileUtil.getName(f1), sheetIdx);
|
|
|
|
|
int start_row = record.getInt("start_row");
|
|
|
|
|
int end_row = record.getInt("end_row");
|
|
|
|
|
int start_column = record.getInt("start_column");
|
|
|
|
@ -144,8 +144,8 @@ public class ExcelUtil {
|
|
|
|
|
String column_name, memo;
|
|
|
|
|
for (int colNum = 0; colNum < ed.getLastCellNum(); colNum++) {
|
|
|
|
|
if (StrKit.isBlank(ed.getCell(colNum).toString())) {
|
|
|
|
|
memo = removeKuoHao(st.getCell(colNum).toString().replace("\n", ""));
|
|
|
|
|
column_name = ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(st.getCell(colNum).toString()));
|
|
|
|
|
memo = CommonUtil.removeKuoHao(st.getCell(colNum).toString().replace("\n", ""));
|
|
|
|
|
column_name = ChineseCharacterUtil.getColumnNameByMemo(CommonUtil.removeKuoHao(st.getCell(colNum).toString()));
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("memo", memo);
|
|
|
|
|
record.set("column_name", column_name);
|
|
|
|
@ -155,8 +155,8 @@ public class ExcelUtil {
|
|
|
|
|
while (StrKit.isBlank(st.getCell(k).toString())) k--;
|
|
|
|
|
memo = st.getCell(k).toString().replace("\n", "")
|
|
|
|
|
+ "_" + ed.getCell(colNum).toString().replace("\n", "");
|
|
|
|
|
column_name = ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(st.getCell(k).toString()))
|
|
|
|
|
+ "_" + ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(ed.getCell(colNum).toString()));
|
|
|
|
|
column_name = ChineseCharacterUtil.getColumnNameByMemo(CommonUtil.removeKuoHao(st.getCell(k).toString()))
|
|
|
|
|
+ "_" + ChineseCharacterUtil.getColumnNameByMemo(CommonUtil.removeKuoHao(ed.getCell(colNum).toString()));
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("memo", memo);
|
|
|
|
|
record.set("column_name", column_name.toLowerCase());
|
|
|
|
@ -178,168 +178,6 @@ public class ExcelUtil {
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:PG数据库数据类型与EXCEL中数据类型的转换关系
|
|
|
|
|
*
|
|
|
|
|
* @param pgColumnDataType
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String convertDataType(String pgColumnDataType) {
|
|
|
|
|
String res = "String";
|
|
|
|
|
if (pgColumnDataType.startsWith("varchar")) res = "String";
|
|
|
|
|
else if (pgColumnDataType.startsWith("int")) res = "Integer";
|
|
|
|
|
else if (pgColumnDataType.startsWith("double")) res = "Double";
|
|
|
|
|
else if (pgColumnDataType.startsWith("date")) res = "Date";
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:创建表
|
|
|
|
|
*
|
|
|
|
|
* @param tableName
|
|
|
|
|
* @param input
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Kv createTable(String tableName, Kv input) {
|
|
|
|
|
List<Record> list = (List<Record>) input.get("list");
|
|
|
|
|
String sheetName = input.getStr("sheet_name");
|
|
|
|
|
String upload_excel_filename = input.getStr("upload_excel_filename");
|
|
|
|
|
String colSql = "", commentSql = "";
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
String column_name = record.getStr("column_name").toLowerCase();
|
|
|
|
|
colSql += column_name + " " + record.getStr("column_type");//varchar(2048)
|
|
|
|
|
String memo = record.getStr("memo");
|
|
|
|
|
if (record.getBoolean("allow_blank") == null || !record.getBoolean("allow_blank")) colSql += " NOT NULL ";
|
|
|
|
|
colSql += ",";
|
|
|
|
|
commentSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"" + column_name + "\" IS '" + memo + "';\n";
|
|
|
|
|
}
|
|
|
|
|
String finalSql = "CREATE TABLE \"public\".\"" + tableName + "\" (";
|
|
|
|
|
finalSql += "\"id\" serial4,";
|
|
|
|
|
finalSql += "\"bureau_id\" char(36) NOT NULL,";
|
|
|
|
|
finalSql += "\"person_id\" char(36) NOT NULL,";
|
|
|
|
|
finalSql += colSql;
|
|
|
|
|
finalSql += "PRIMARY KEY (\"id\")";
|
|
|
|
|
finalSql += ");\n";
|
|
|
|
|
|
|
|
|
|
finalSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"id\" IS '主键,自增长ID';\n";
|
|
|
|
|
finalSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"bureau_id\" IS '单位ID';\n";
|
|
|
|
|
finalSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"person_id\" IS '上传人员ID';\n";
|
|
|
|
|
finalSql += commentSql;
|
|
|
|
|
finalSql += "COMMENT ON TABLE \"public\".\"" + tableName + "\" IS '" + sheetName + "';";
|
|
|
|
|
//添加索引
|
|
|
|
|
finalSql += "CREATE INDEX ON \"public\".\"" + tableName + "\" (\"bureau_id\");";
|
|
|
|
|
finalSql += "CREATE INDEX ON \"public\".\"" + tableName + "\" (\"person_id\");";
|
|
|
|
|
Db.update(finalSql);
|
|
|
|
|
|
|
|
|
|
//写入模板与表结构的关系t_importexcel_mapping
|
|
|
|
|
String sql = "delete from t_importexcel_mapping where table_name=?";
|
|
|
|
|
Db.update(sql, tableName);
|
|
|
|
|
|
|
|
|
|
int sheet_index = input.getInt("sheet_index");
|
|
|
|
|
String sheet_name = input.getStr("sheet_name");
|
|
|
|
|
List<Record> writeList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("table_name", tableName);
|
|
|
|
|
record.set("column_name", list.get(i).getStr("column_name"));
|
|
|
|
|
record.set("excel_column_idx", i);
|
|
|
|
|
record.set("memo", list.get(i).getStr("memo"));
|
|
|
|
|
record.set("allow_blank", list.get(i).getBoolean("allow_blank"));
|
|
|
|
|
record.set("column_type", convertDataType(list.get(i).getStr("column_type")));
|
|
|
|
|
record.set("upload_excel_filename", upload_excel_filename);
|
|
|
|
|
record.set("sheet_index", sheet_index);
|
|
|
|
|
record.set("sheet_name", sheet_name);
|
|
|
|
|
record.set("options", list.get(i).getStr("options"));
|
|
|
|
|
writeList.add(record);
|
|
|
|
|
}
|
|
|
|
|
Db.batchSave("t_importexcel_mapping", writeList, 100);
|
|
|
|
|
|
|
|
|
|
//记录模板文件的头文件位置,开始行,结束行,开始列,结束列
|
|
|
|
|
sql = "delete from t_importexcel_config where upload_excel_filename=? and table_name=?";
|
|
|
|
|
Db.update(sql, upload_excel_filename, tableName);
|
|
|
|
|
|
|
|
|
|
int start_row = input.getInt("start_row");
|
|
|
|
|
int end_row = input.getInt("end_row");
|
|
|
|
|
int start_column = input.getInt("start_column");
|
|
|
|
|
int end_column = input.getInt("end_column");
|
|
|
|
|
int data_start_row = input.getInt("data_start_row");
|
|
|
|
|
int column_num = input.getInt("column_num");
|
|
|
|
|
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("upload_excel_filename", upload_excel_filename);
|
|
|
|
|
record.set("table_name", tableName);
|
|
|
|
|
record.set("start_row", start_row);
|
|
|
|
|
record.set("end_row", end_row);
|
|
|
|
|
record.set("start_column", start_column);
|
|
|
|
|
record.set("end_column", end_column);
|
|
|
|
|
record.set("sheet_index", sheet_index);
|
|
|
|
|
record.set("sheet_name", sheet_name);
|
|
|
|
|
record.set("data_start_row", data_start_row);
|
|
|
|
|
record.set("column_num", column_num);
|
|
|
|
|
Db.save("t_importexcel_config", "id", record);
|
|
|
|
|
|
|
|
|
|
Kv kv = Kv.create();
|
|
|
|
|
kv.set("success", true);
|
|
|
|
|
kv.set("message", "表结构创建成功!");
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:删除表
|
|
|
|
|
*
|
|
|
|
|
* @param tableName
|
|
|
|
|
*/
|
|
|
|
|
public static void dropTable(String tableName) {
|
|
|
|
|
String sql = "drop table if exists " + tableName;
|
|
|
|
|
Db.update(sql);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断表是不是存在
|
|
|
|
|
*
|
|
|
|
|
* @param tableName
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isTableExist(String tableName) {
|
|
|
|
|
String sql = "select count(*) as c from pg_class where relname = ?";
|
|
|
|
|
return Db.findFirst(sql, tableName).getInt("c") == 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定模板+指定Sheet的全部配置信息
|
|
|
|
|
*
|
|
|
|
|
* @param upload_excel_filename
|
|
|
|
|
* @param sheet_index
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Record getSheetConfig(String upload_excel_filename, int sheet_index) {
|
|
|
|
|
String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?";
|
|
|
|
|
return Db.findFirst(sql, upload_excel_filename, sheet_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定模板+指定Sheet的个数
|
|
|
|
|
*
|
|
|
|
|
* @param upload_excel_filename
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int getSheetCount(String upload_excel_filename) {
|
|
|
|
|
String sql = "select * from t_importexcel_config where upload_excel_filename=?";
|
|
|
|
|
List<Record> list = Db.find(sql, upload_excel_filename);
|
|
|
|
|
return list.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定模板+指定Sheet中所有列的限制条件
|
|
|
|
|
*
|
|
|
|
|
* @param upload_excel_filename
|
|
|
|
|
* @param sheet_index
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static List<Record> getSheetMapping(String upload_excel_filename, int sheet_index) {
|
|
|
|
|
String sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
|
|
|
|
|
return Db.find(sql, upload_excel_filename, sheet_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:对于指定Cell中文字进行加红色(*)提醒
|
|
|
|
@ -531,52 +369,7 @@ public class ExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断字符串是不是能转换为整数
|
|
|
|
|
*
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断一个单元格是不是被合并了
|
|
|
|
|
*
|
|
|
|
@ -594,102 +387,4 @@ public class ExcelUtil {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:移除小括号,以及小括号内的文字内容
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String removeKuoHao(String str) {
|
|
|
|
|
str = str.replace("\n", "");
|
|
|
|
|
String output = str.replaceAll("\\(.*?\\)", "");
|
|
|
|
|
output = output.replaceAll("\\(.*?\\)", "");
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:导入数据
|
|
|
|
|
*
|
|
|
|
|
* @throws ParseException
|
|
|
|
|
*/
|
|
|
|
|
public static void importData(String upload_excel_filename, XSSFWorkbook wb,
|
|
|
|
|
int sheetIdx, String bureau_id, String person_id) throws ParseException {
|
|
|
|
|
//读取sheet页
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
//通过表名获取到它的读取起始行,终止列
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
//先删除
|
|
|
|
|
sql = "delete from " + table_name + " where bureau_id=?";
|
|
|
|
|
Db.update(sql, bureau_id);
|
|
|
|
|
|
|
|
|
|
int data_start_row = record.getInt("data_start_row");
|
|
|
|
|
int column_num = record.getInt("column_num");
|
|
|
|
|
|
|
|
|
|
//获取字段与EXCEL列的映射信息
|
|
|
|
|
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");
|
|
|
|
|
String column_name = r.getStr("column_name");
|
|
|
|
|
String column_type = r.getStr("column_type");
|
|
|
|
|
Record rt = new Record();
|
|
|
|
|
rt.set("column_name", column_name);
|
|
|
|
|
rt.set("column_type", column_type);
|
|
|
|
|
_map.put(excel_column_idx, rt);
|
|
|
|
|
}
|
|
|
|
|
//开始读取数据
|
|
|
|
|
List<Record> writeList = new ArrayList<>();
|
|
|
|
|
for (int i = data_start_row; i <= sheet.getLastRowNum(); 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++) {
|
|
|
|
|
XSSFCell cell = row.getCell(j);
|
|
|
|
|
String colType = _map.get(j).getStr("column_type");
|
|
|
|
|
String colName = _map.get(j).getStr("column_name");
|
|
|
|
|
|
|
|
|
|
//cell可能是被合并的单元格
|
|
|
|
|
if (isMerged(sheet, cell)) {
|
|
|
|
|
String value = ExcelUtil.getValue(cell).toString();
|
|
|
|
|
if (StrKit.isBlank(value)) {
|
|
|
|
|
for (int k = i - 1; ; k--) {
|
|
|
|
|
String prev = ExcelUtil.getValue(sheet.getRow(k).getCell(j)).toString();
|
|
|
|
|
if (!StrKit.isBlank(prev)) {
|
|
|
|
|
value = prev;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeRecord.set(colName, value);
|
|
|
|
|
} else {
|
|
|
|
|
if (colType.equals("Integer"))
|
|
|
|
|
writeRecord.set(colName, (int) Double.parseDouble(getValue(cell).toString()));
|
|
|
|
|
else if (colType.equals("String"))
|
|
|
|
|
writeRecord.set(colName, getValue(cell).toString());
|
|
|
|
|
else if (colType.equals("Double"))
|
|
|
|
|
writeRecord.set(colName, Double.parseDouble(getValue(cell).toString()));
|
|
|
|
|
else if (colType.equals("Date")) {
|
|
|
|
|
String dateString = getValue(cell).toString();
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
Date date = dateFormat.parse(dateString);
|
|
|
|
|
writeRecord.set(colName, date);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeList.add(writeRecord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 写入数据
|
|
|
|
|
Db.batchSave(table_name, writeList, 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|