|
|
|
@ -19,18 +19,6 @@ import java.util.*;
|
|
|
|
|
import static org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted;
|
|
|
|
|
|
|
|
|
|
public class ExcelUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:移除小括号,以及小括号内的文字内容
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String removeKuoHao(String str) {
|
|
|
|
|
str = str.replace("\n", "");
|
|
|
|
|
String output = str.replaceAll("\\(.*?\\)", "");
|
|
|
|
|
output = output.replaceAll("\\(.*?\\)", "");
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* )
|
|
|
|
@ -88,53 +76,6 @@ public class ExcelUtil {
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断表是不是存在
|
|
|
|
|
*
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:检查两个EXCEL文件的指定行+指定列是不是内容一致,返回值是不一致的位置集合,位置用数对 Map.Entry<Integer, Integer>描述
|
|
|
|
|
*
|
|
|
|
@ -352,107 +293,51 @@ public class ExcelUtil {
|
|
|
|
|
String sql = "drop table if exists " + tableName;
|
|
|
|
|
Db.update(sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断一个单元格是不是被合并了
|
|
|
|
|
* 功能:判断表是不是存在
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
* @param tableName
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) {
|
|
|
|
|
// 判断单元格是否被合并
|
|
|
|
|
for (CellRangeAddress range : sheet.getMergedRegions()) {
|
|
|
|
|
if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow()
|
|
|
|
|
&& cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
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的全部配置信息
|
|
|
|
|
*
|
|
|
|
|
* @throws ParseException
|
|
|
|
|
* @param upload_excel_filename
|
|
|
|
|
* @param sheet_index
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
//通过表名获取到它的读取起始行,终止列
|
|
|
|
|
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=?";
|
|
|
|
|
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);
|
|
|
|
|
return Db.findFirst(sql, upload_excel_filename, sheet_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//遍历列
|
|
|
|
|
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");
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定模板+指定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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定模板+指定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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -692,4 +577,119 @@ public class ExcelUtil {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断一个单元格是不是被合并了
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) {
|
|
|
|
|
// 判断单元格是否被合并
|
|
|
|
|
for (CellRangeAddress range : sheet.getMergedRegions()) {
|
|
|
|
|
if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow()
|
|
|
|
|
&& cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|