main
黄海 2 years ago
parent d959e95c8a
commit 9abe5ef820

@ -0,0 +1,457 @@
package UnitTest.ImportExcel.Backup;
import UnitTest.ImportExcel.Bean.CellBean;
import com.dsideal.QingLong.Util.ChineseCharacterUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class createTable {
/**
*
*
* @param input
* @return
*/
public static String removeKuoHao(String input) {
String output = input.replaceAll("\\(.*?\\)", "");
output = output.replaceAll("\\.*?\\", "");
return output;
}
/**
*
*
* @param input
* @return
*/
public static String removeNumbers(String input) {
String regex = "\\d+"; // 匹配数字的正则表达式
String result = input.replaceAll(regex, ""); // 使用replaceAll方法将匹配到的数字替换为空字符串
return result;
}
/**
* )
* vo
*
* @param cell
* @return
*/
private static CellBean getCellBean(Cell cell) {
CellBean dto = new CellBean();//创建单元格对象
if (cell != null) {
//值
switch (cell.getCellType()) {
case NUMERIC://导入数据,没有支持到时间的,一般都是到日期为止
if (DateUtil.isCellDateFormatted(cell)) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone());
dto.setValue(sdf.format(cell.getDateCellValue()));
dto.setType("Date");
} else {
String type;
Long longVal = Math.round(cell.getNumericCellValue());
Double doubleVal = cell.getNumericCellValue();
if (Double.parseDouble(longVal + ".0") == doubleVal) { //判断是否含有小数位.0
type = "int";
} else {
type = "float8";
}
DataFormatter formatter = new DataFormatter();
dto.setValue(formatter.formatCellValue(cell));
dto.setType(type);
}
break;
case STRING:
dto.setValue(cell.getStringCellValue());
dto.setType("varchar(1024)");
break;
case BOOLEAN:
dto.setValue(String.valueOf(cell.getBooleanCellValue()));
dto.setType("bool");
break;
case FORMULA:
dto.setValue(cell.getCellFormula());
dto.setType("varchar(1024)");
break;
case BLANK:
dto.setValue("");
dto.setType("varchar(1024)");
break;
case ERROR:
dto.setValue(ErrorEval.getText(cell.getErrorCellValue()));
dto.setType("varchar(1024)");
break;
default: {
dto.setValue("Unknown Cell Type: " + cell.getCellType());
dto.setType("varchar(1024)");
}
}
// xlsx 07版
//背景颜色
CellStyle cellStyle = cell.getCellStyle();
XSSFColor xssfColor = (XSSFColor) cellStyle.getFillForegroundColorColor();
byte[] bytes;
if (xssfColor != null) {
bytes = xssfColor.getRGB();
dto.setBackgroundColor(String.format("#%02X%02X%02X", bytes[0], bytes[1], bytes[2]));
}
}
return dto;
}
/**
* Sheet,Sheet
*
* @param sheet
* @return
*/
public static List<Integer> getHead(XSSFSheet sheet) {
List<Integer> _list = new ArrayList<>();
//整行都是同一种颜色的,视为表头
// 遍历行
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
//获得行
Row row = sheet.getRow(i);
//遍历列
if (row != null) {
Map<String, Integer> _map = new HashMap<>();
for (int j = 0; j < row.getLastCellNum(); j++) {
//获取单元格
Cell cell = row.getCell(j);
if (cell == null) continue;
CellBean eo = getCellBean(cell);
if (_map.containsKey(eo.getBackgroundColor()))
_map.put(eo.getBackgroundColor(), _map.get(eo.getBackgroundColor()) + 1);
else
_map.put(eo.getBackgroundColor(), 1);
}
if (_map.size() == 1 && _map.entrySet().iterator().next().getKey().startsWith("#")) {
_list.add(i + 1);
}
}
}
return _list;
}
/**
*
*
* @param sheet
* @param st
* @param ed
* @return
*/
public static List<List<String>> getStruct(XSSFSheet sheet, int st, int ed) {
List<List<String>> list = new ArrayList<>();
// 遍历行
for (int i = st; i <= ed; i++) {
//获得行
Row row = sheet.getRow(i);
List<String> r = new ArrayList<>();
//遍历列
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (cell == null) continue;
CellBean eo = getCellBean(cell);
r.add(eo.getType());
}
}
list.add(r);
}
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;
}
/**
* EXCEL+ Map.Entry<Integer, Integer>
*
* @param f1
* @param f2
* @param startRow
* @param endRow
* @param startCol
* @param endCol
* @return
* @throws IOException
*/
public static List<Map.Entry<Integer, Integer>> checkYiZhi(String f1, String f2, int startRow, int endRow, int startCol, int endCol) throws IOException {
FileInputStream file1 = new FileInputStream(f1);
FileInputStream file2 = new FileInputStream(f2);
List<Map.Entry<Integer, Integer>> errList = new ArrayList<>();
Workbook wb1 = WorkbookFactory.create(file1);
Workbook wb2 = WorkbookFactory.create(file2);
Sheet sheet1 = wb1.getSheetAt(0);
Sheet sheet2 = wb2.getSheetAt(0);
for (int i = startRow; i <= endRow; i++) {
Row row1 = sheet1.getRow(i);
Row row2 = sheet2.getRow(i);
for (int j = startCol; j <= endCol; j++) {
Cell cell1 = row1.getCell(j);
Cell cell2 = row2.getCell(j);
if (cell1 != null && cell2 != null) {
if (!cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
Map.Entry<Integer, Integer> pair = new AbstractMap.SimpleEntry<>((i + 1), (j + 1));
errList.add(pair);
}
}
}
}
file1.close();
file2.close();
return errList;
}
/**
*
*
* @param tableName
* @param filePath excel
* @throws IOException
*/
public static Kv createTable(String tableName, String filePath) throws IOException {
Kv kv = Kv.create();
if (isTableExist(tableName)) {
kv.set("success", false);
kv.set("message", "表名:" + tableName + "已存在,不能创建,请手动更换!");
return kv;
}
String colSql = "", commentSql = "";
InputStream is = new FileInputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook(is);
//读取sheet页
XSSFSheet sheet = wb.getSheetAt(0);
//找到表头
List<Integer> _list = getHead(sheet);
//遍历每一列,获取列信息,数据类型
List<List<String>> a = getStruct(sheet, _list.get(_list.size() - 1) + 1, sheet.getLastRowNum() + 1);
List<String> b = a.get(0);
// 输出表头的文字
Row ed = sheet.getRow(_list.get(_list.size() - 1) - 1);//下标从0开始
Row st = sheet.getRow(_list.get(0) - 1);//下标从0开始
//非空列有哪些
Set<Integer> notNullSet = new HashSet<>();
// 首行数据
Row dataFirstRow = sheet.getRow(_list.get(_list.size() - 1));
for (int i = 0; i < dataFirstRow.getLastCellNum(); i++) {
Cell cell = dataFirstRow.getCell(i);
CellBean eo = getCellBean(cell);
if (eo.getBackgroundColor().equals("#FF0000")) {
notNullSet.add(i + 1);
}
}
//需要记录此模板编号的第几列对应哪个字段,方便以后的数据写入
Record _map[] = new Record[ed.getLastCellNum()];
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());
column_name = ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(st.getCell(colNum).toString()));
} else {
int k = colNum;
while (StrKit.isBlank(st.getCell(k).toString())) k--;
memo = removeKuoHao(st.getCell(k).toString())
+ "_" + removeKuoHao(ed.getCell(colNum).toString());
column_name = ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(st.getCell(k).toString()))
+ "_" + ChineseCharacterUtil.getColumnNameByMemo(removeKuoHao(ed.getCell(colNum).toString()));
}
Record r = new Record();
r.set("memo", memo);
r.set("column_name", column_name);
r.set("column_type", b.get(colNum));
_map[colNum] = r;
colSql += "\"" + column_name + "\" " + b.get(colNum);
if (notNullSet.contains(colNum + 1)) 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 += colSql;
finalSql += "PRIMARY KEY (\"id\")";
finalSql += ");\n";
finalSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"id\" IS '主键自增长ID';\n";
finalSql += commentSql;
Db.update(finalSql);
//写入模板与表结构的关系t_importexcel_mapping
String sql = "delete from t_importexcel_mapping where table_name=?";
Db.update(sql, tableName);
List<Record> list = new ArrayList<>();
for (int i = 0; i < _map.length; i++) {
Record record = new Record();
record.set("table_name", tableName);
record.set("column_name", _map[i].getStr("column_name"));
record.set("excel_column_idx", i + 1);
record.set("memo", _map[i].getStr("memo"));
record.set("column_type", removeNumbers(removeKuoHao(_map[i].getStr("column_type"))));
list.add(record);
}
Db.batchSave("t_importexcel_mapping", list, 100);
//写入t_importexcel_config
sql = "delete from t_importexcel_config where table_name=?";
Db.update(sql, tableName);
Record record = new Record();
record.set("table_name", tableName);
record.set("excel_module_filename", "先不写上去");
record.set("data_start_row", _list.get(_list.size() - 1) + 1);
record.set("column_num", ed.getLastCellNum());
Db.save("t_importexcel_config", "table_name", record);
wb.close();
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 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);
//读取sheet页
XSSFSheet sheet = wb.getSheetAt(0);
//通过表名获取到它的读取起始行,终止列
String sql = "select * from t_importexcel_config where table_name=?";
Record record = Db.findFirst(sql, 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);
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 - 1; i <= sheet.getLastRowNum(); i++) {
//获得行
Row row = sheet.getRow(i);
//遍历列
if (row != null) {
Record writeRecord = new Record();
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();
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);
//关闭excel
wb.close();
}
public static void main(String[] args) throws Exception {
//告之配置文件位置
PropKit.use("application.properties");
HikariCpPlugin hp = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
PropKit.get("password").trim(), PropKit.get("driverClassName"));
hp.start();
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(hp);
//配置默认小写
arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
arp.setDialect(new PostgreSqlDialect());
arp.start();
//测试两个导入模板
String workingPath = "D:\\dsWork\\QingLong\\src\\main\\java\\UnitTest\\ImportExcel\\";
String filePath = workingPath + "测试1.xlsx";
String tableName = "ds_gtzz_t_bmwj";//表名研发人员在界面上录入程序需要检查此表名是不是存在如果存在返回错误信息不存在则可以生成创建表的SQL语句
dropTable(tableName);
createTable(tableName, filePath);
importData(tableName, filePath);
filePath = workingPath + "测试2.xlsx";
tableName = "ds_gtzz_t_zc";//表名研发人员在界面上录入程序需要检查此表名是不是存在如果存在返回错误信息不存在则可以生成创建表的SQL语句
dropTable(tableName);
createTable(tableName, filePath);
importData(tableName, filePath);
System.out.println("恭喜,所有操作成功完成!");
}
}

@ -1,27 +1,35 @@
package UnitTest.ImportExcel.Bean;
/**
* vo
*/
public class CellBean {
private String value = "";//值
private String backgroundColor = "";//背景色
private String fontColor = "";//文字颜色
/**
* vo
*/
public class CellBean {
private String value = "";//值
private String backgroundColor = "";//背景色
private String type="";
public String getValue() {
return value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void setValue(String value) {
this.value = value;
}
public String getBackgroundColor() {
return backgroundColor;
}
public String getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
}
public String getType() {
return type;
}
}
public void setType(String type) {
this.type = type;
}
}

@ -0,0 +1,49 @@
package UnitTest.ImportExcel;
import com.aspose.cells.Workbook;
import com.dsideal.QingLong.Util.AsposeUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ImportExcelData {
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
//模拟上传文件的文件名称
public static String upload_excel_filename = "b61a2af2-223f-4058-a675-b212e4dd9487" + ".xlsx";
public static void main(String[] args) throws IOException {
//加载License
AsposeUtil.getLicense();
//告之配置文件位置
PropKit.use("application.properties");
HikariCpPlugin hp = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
PropKit.get("password").trim(), PropKit.get("driverClassName"));
hp.start();
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(hp);
//配置默认小写
arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
arp.setDialect(new PostgreSqlDialect());
arp.start();
//源文件
String source = path + File.separator + upload_excel_filename;
//解析上传EXCEL中的每个Sheet解析出表头信息表名描述等信息
InputStream is = new FileInputStream(source);
XSSFWorkbook wb = new XSSFWorkbook(is);
//关闭POI的工作簿
wb.close();
}
}

@ -193,6 +193,20 @@ public class GenericTemplateUtil {
return kv;
}
/**
* PGEXCEL
* @param pgColumnDataType
* @return
*/
public static String convertDataType(String pgColumnDataType) {
String res = "String";
if (pgColumnDataType.startsWith("varchar")) res = "String";
else if (pgColumnDataType.equals("int")) res = "Integer";
else if (pgColumnDataType.equals("double")) res = "Double";
else if (pgColumnDataType.equals("date")) res = "Date";
return res;
}
/**
*
*
@ -240,7 +254,7 @@ public class GenericTemplateUtil {
record.set("column_name", list.get(i).getStr("column_name"));
record.set("excel_column_idx", i + 1);
record.set("memo", list.get(i).getStr("memo"));
record.set("column_type", list.get(i).getStr("column_type"));
record.set("column_type", convertDataType(list.get(i).getStr("column_type")));
record.set("upload_excel_filename", upload_excel_filename);
writeList.add(record);
}

Loading…
Cancel
Save