main
黄海 2 years ago
parent 57e1080d01
commit ad3285029d

@ -2,6 +2,7 @@ package UnitTest.ImportExcel;
import cn.hutool.core.io.FileUtil;
import com.dsideal.QingLong.Util.ExcelUtil;
import com.dsideal.QingLong.Util.ImportExcelKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
@ -20,7 +21,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class CreateTableAndTemplate {
public class CreateTable {
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
//用户上传的模板
@ -113,13 +114,13 @@ public class CreateTableAndTemplate {
//开发时暴力删除掉原表,在正式上线前要注意:不能简单粗暴的删除!!
for (Kv kv : kvList) {
String tableName = kv.getStr("table_name");
ExcelUtil.dropTable(tableName);
ImportExcelKit.dropTable(tableName);
}
//判断表是不是已存在
for (Kv kv : kvList) {
String tableName = kv.getStr("table_name");
if (ExcelUtil.isTableExist(tableName)) {
if (ImportExcelKit.isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在,不能删除,程序无法继续!");
return;
}
@ -128,7 +129,7 @@ public class CreateTableAndTemplate {
//检查通过,处理建表逻辑
for (Kv kv : kvList) {
String tableName = kv.getStr("table_name");
ExcelUtil.createTable(tableName, kv);//创建表
ImportExcelKit.createTable(tableName, kv);//创建表
}
//检查是不是需要对指定列进行加红星+Comment操作

@ -1,6 +1,8 @@
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.CommonUtil;
import com.dsideal.QingLong.Util.ExcelUtil;
import com.dsideal.QingLong.Util.ImportExcelKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
@ -20,7 +22,7 @@ import java.util.List;
import java.util.Map;
//目前想到如果用户克隆了一个任务,那个就拷贝出一个模板文件即可,这个模板文件就是我生成的带下拉框、红星的模板文件。
public class ImportExcelData {
public class ImportExcel {
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
//模拟上传文件的文件名称
@ -50,7 +52,7 @@ public class ImportExcelData {
String f2 = path + File.separator + "school.xlsx";
//对比两个EXCEL文件 是不是格式一致,也就是是不是上传了正确的模板文件
int sheetCnt = ExcelUtil.getSheetCount(upload_excel_filename);
int sheetCnt = ImportExcelKit.getSheetCount(upload_excel_filename);
for (int i = 0; i < sheetCnt; i++) {
List<Map.Entry<Integer, Integer>> chayi = ExcelUtil.checkYiZhi(f1, f2, i);
if (chayi.size() > 0) {
@ -71,7 +73,7 @@ public class ImportExcelData {
//移除所有批注
ExcelUtil.RemoveAllComment(sheet);
//数据起始行
Record r = ExcelUtil.getSheetConfig(upload_excel_filename, i);
Record r = ImportExcelKit.getSheetConfig(upload_excel_filename, i);
int data_start_row = r.getInt("data_start_row");
//恢复背景色
ExcelUtil.resetStyle(wb, sheet, data_start_row);
@ -79,7 +81,7 @@ public class ImportExcelData {
//数据有效行数
int lastRowNum = sheet.getLastRowNum();
//遍历每一列
List<Record> list = ExcelUtil.getSheetMapping(upload_excel_filename, i);
List<Record> list = ImportExcelKit.getSheetMapping(upload_excel_filename, i);
for (Record record : list) {//列
int excel_column_idx = record.getInt("excel_column_idx");//第几列
String column_type = record.getStr("column_type");//类型
@ -115,15 +117,15 @@ public class ImportExcelData {
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
Object obj = ExcelUtil.getValue(cell);
if (column_type.equals("Integer") && !ExcelUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
if (column_type.equals("Integer") && !CommonUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
ExcelUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!");
ExcelUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
} else if (column_type.equals("Double") && !ExcelUtil.isDouble(obj.toString())) {//要求是浮点数,实际不是浮点数
} else if (column_type.equals("Double") && !CommonUtil.isDouble(obj.toString())) {//要求是浮点数,实际不是浮点数
ExcelUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!");
ExcelUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
} else if (column_type.equals("Date") && !ExcelUtil.isDate(obj.toString())) {//要求是日期,实际不是日期
} else if (column_type.equals("Date") && !CommonUtil.isDate(obj.toString())) {//要求是日期,实际不是日期
ExcelUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!");
ExcelUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
@ -148,7 +150,7 @@ public class ImportExcelData {
}
// 检查通过,导入数据
for (int i = 0; i < sheetCnt; i++) ExcelUtil.importData(upload_excel_filename, wb, i, bureau_id, person_id);
for (int i = 0; i < sheetCnt; i++) ImportExcelKit.importData(upload_excel_filename, wb, i, bureau_id, person_id);
System.out.println("恭喜,所有数据成功导入!");
//关闭Excel

@ -1,4 +1,4 @@
package UnitTest.ImportExcel;
package UnitTest.ImportExcel.Test;
import com.aspose.cells.Cell;
import com.aspose.cells.Workbook;

@ -636,4 +636,64 @@ public class CommonUtil {
String domainPattern = "^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{1,63}(?<!-)(\\.[a-zA-Z]{2,})+$";
return Pattern.matches(domainPattern, str);
}
/**
*
*
* @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;
}
}
/**
*
*
* @param str
* @return
*/
public static String removeKuoHao(String str) {
str = str.replace("\n", "");
String output = str.replaceAll("\\(.*?\\)", "");
output = output.replaceAll("\\.*?\\", "");
return output;
}
}

@ -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;
}
/**
* PGEXCEL
*
* @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);
}
}

@ -0,0 +1,264 @@
package com.dsideal.QingLong.Util;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ImportExcelKit {
/**
* PGEXCEL
*
* @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);
}
/**
*
*
* @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 (ExcelUtil.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(ExcelUtil.getValue(cell).toString()));
else if (colType.equals("String"))
writeRecord.set(colName, ExcelUtil.getValue(cell).toString());
else if (colType.equals("Double"))
writeRecord.set(colName, Double.parseDouble(ExcelUtil.getValue(cell).toString()));
else if (colType.equals("Date")) {
String dateString = ExcelUtil.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);
}
}
Loading…
Cancel
Save