|
|
|
@ -5,10 +5,7 @@ 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.ss.formula.eval.ErrorEval;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
|
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;
|
|
|
|
@ -16,12 +13,11 @@ 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 ExcelUtil {
|
|
|
|
|
public class GenericTemplateUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:移除小括号,以及小括号内的文字内容
|
|
|
|
|
*
|
|
|
|
@ -35,18 +31,6 @@ public class ExcelUtil {
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:移除字符串中的数字
|
|
|
|
|
*
|
|
|
|
|
* @param input
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String removeNumbers(String input) {
|
|
|
|
|
String regex = "\\d+"; // 匹配数字的正则表达式
|
|
|
|
|
String result = input.replaceAll(regex, ""); // 使用replaceAll方法将匹配到的数字替换为空字符串
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* )
|
|
|
|
|
* 获取单元格vo
|
|
|
|
@ -57,54 +41,6 @@ public class ExcelUtil {
|
|
|
|
|
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();
|
|
|
|
@ -124,7 +60,7 @@ public class ExcelUtil {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static List<Integer> getHead(XSSFSheet sheet) {
|
|
|
|
|
List<Integer> _list = new ArrayList<>();
|
|
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
|
|
//整行都是同一种非空白颜色,视为表头
|
|
|
|
|
// 遍历行
|
|
|
|
|
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
@ -138,44 +74,16 @@ public class ExcelUtil {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
@ -235,16 +143,18 @@ public class ExcelUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取表结构信息
|
|
|
|
|
*/
|
|
|
|
|
public static Kv getTableStructInfo(XSSFWorkbook wb, int sheetIdx) {
|
|
|
|
|
public static Kv getTableStruct(XSSFWorkbook wb, int sheetIdx) {
|
|
|
|
|
Kv kv = Kv.create();
|
|
|
|
|
//读取sheet页
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
//找到表头
|
|
|
|
|
List<Integer> _list = getHead(sheet);
|
|
|
|
|
//表名称
|
|
|
|
|
String sheetName = wb.getSheetName(sheetIdx);
|
|
|
|
|
//只允许1行或2行
|
|
|
|
|
if (_list.size() == 0 || _list.size() > 2) {
|
|
|
|
|
kv.set("success", false);
|
|
|
|
|
kv.set("message", "表 “" + wb.getSheetName(sheetIdx) + "” 没有正确设置表头背景色,要求表头可以为一行或两行,而且必须背景色是同一种非空白颜色!");
|
|
|
|
|
kv.set("message", "表 “" + sheetName + "” 没有正确设置表头背景色,要求表头可以为一行或两行,而且必须背景色是同一种非空白颜色!");
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
// 标题,定义为表头上面的部分
|
|
|
|
@ -275,10 +185,11 @@ public class ExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
kv.set("success", true);
|
|
|
|
|
kv.set("message", "信息获取成功!");
|
|
|
|
|
kv.set("message", "表结构获取成功!");
|
|
|
|
|
kv.set("list", list);
|
|
|
|
|
kv.set("data_start_row", ed.getRowNum() + 1);//真实数据的起始行索引
|
|
|
|
|
kv.set("column_num", ed.getLastCellNum());//一共多少列
|
|
|
|
|
kv.set("sheet_name", sheetName);
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -289,9 +200,9 @@ public class ExcelUtil {
|
|
|
|
|
* @param input
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Kv createTable(String tableName, Kv input) {
|
|
|
|
|
public static Kv createTable(String tableName, Kv input, String upload_excel_filename) {
|
|
|
|
|
List<Record> list = (List<Record>) input.get("list");
|
|
|
|
|
|
|
|
|
|
String sheetName = input.getStr("sheet_name");
|
|
|
|
|
String colSql = "", commentSql = "";
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
String column_name = record.getStr("column_name");
|
|
|
|
@ -309,35 +220,26 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
finalSql += "COMMENT ON COLUMN \"public\".\"" + tableName + "\".\"id\" IS '主键,自增长ID';\n";
|
|
|
|
|
finalSql += commentSql;
|
|
|
|
|
finalSql += "COMMENT ON TABLE \"public\".\"" + tableName + "\" IS '" + sheetName + "';";
|
|
|
|
|
|
|
|
|
|
Db.update(finalSql);
|
|
|
|
|
|
|
|
|
|
//写入模板与表结构的关系t_importexcel_mapping
|
|
|
|
|
String sql = "delete from t_importexcel_mapping where table_name=?";
|
|
|
|
|
Db.update(sql, tableName);
|
|
|
|
|
|
|
|
|
|
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", record.getStr("column_name"));
|
|
|
|
|
record.set("column_name", list.get(i).getStr("column_name"));
|
|
|
|
|
record.set("excel_column_idx", i + 1);
|
|
|
|
|
record.set("memo", record.getStr("memo"));
|
|
|
|
|
record.set("column_type", record.getStr("column_type"));
|
|
|
|
|
list.add(record);
|
|
|
|
|
record.set("memo", list.get(i).getStr("memo"));
|
|
|
|
|
record.set("column_type", list.get(i).getStr("column_type"));
|
|
|
|
|
record.set("upload_excel_filename", upload_excel_filename);
|
|
|
|
|
writeList.add(record);
|
|
|
|
|
}
|
|
|
|
|
Db.batchSave("t_importexcel_mapping", writeList, 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", input.getInt("data_start_row"));
|
|
|
|
|
record.set("column_num", input.getInt("column_num"));
|
|
|
|
|
Db.save("t_importexcel_config", "table_name", record);
|
|
|
|
|
|
|
|
|
|
Kv kv = Kv.create();
|
|
|
|
|
kv.set("success", true);
|
|
|
|
|
kv.set("message", "表结构创建成功!");
|
|
|
|
@ -419,8 +321,4 @@ public class ExcelUtil {
|
|
|
|
|
//关闭excel
|
|
|
|
|
wb.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println(removeKuoHao("现状_配置(如交互式白板标明尺寸,OPS计算机标明处理器/内存/硬盘等)"));
|
|
|
|
|
}
|
|
|
|
|
}
|