|
|
|
@ -1,188 +0,0 @@
|
|
|
|
|
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;
|
|
|
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
|
|
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
|
|
|
|
|
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class CreateTable {
|
|
|
|
|
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
|
|
|
|
|
|
|
|
|
|
//用户上传的模板
|
|
|
|
|
public static String userUploadExcel = "source.xlsx";
|
|
|
|
|
//我处理后的模板,提供给学校下载用的模板
|
|
|
|
|
public static String upload_excel_filename = "b61a2af2-223f-4058-a675-b212e4dd9487" + ".xlsx";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:模拟前端人机交互对表结构进行修改
|
|
|
|
|
*
|
|
|
|
|
* @param kList
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static void changeData(List<Kv> kList) {
|
|
|
|
|
int sheetIdx = 0;
|
|
|
|
|
for (Kv kv : kList) {
|
|
|
|
|
List<Record> list = (List<Record>) kv.get("list");
|
|
|
|
|
int colIdx = 0;
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
record.set("column_type", "varchar(2048)");
|
|
|
|
|
//1、模拟生成下拉框
|
|
|
|
|
if (sheetIdx == 0 && colIdx == 3) {//第一个Sheet表的品牌型号列
|
|
|
|
|
record.set("options", "华为,中兴,思科");//生成下拉框
|
|
|
|
|
}
|
|
|
|
|
//2、模拟必填写项:台数必需填写+品牌型号必需填写
|
|
|
|
|
if ((sheetIdx == 0 && colIdx == 3) || (sheetIdx == 0 && colIdx == 5))
|
|
|
|
|
record.set("allow_blank", false);
|
|
|
|
|
else
|
|
|
|
|
record.set("allow_blank", true);
|
|
|
|
|
|
|
|
|
|
//3、设置固定数据类型:台数
|
|
|
|
|
if (sheetIdx == 0 && colIdx == 5)
|
|
|
|
|
record.set("column_type", "int4");
|
|
|
|
|
//下一列
|
|
|
|
|
colIdx++;
|
|
|
|
|
}
|
|
|
|
|
sheetIdx++;
|
|
|
|
|
kv.set("table_name", "ds_source_" + sheetIdx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
//开发时暂时写死源文件名称
|
|
|
|
|
//upload_excel_filename = UUID.randomUUID().toString().toLowerCase() + ".xlsx";
|
|
|
|
|
|
|
|
|
|
String source = path + File.separator + upload_excel_filename;
|
|
|
|
|
if (FileUtil.exist(source)) FileUtil.del(source);
|
|
|
|
|
//复制成我自己的,随便折腾
|
|
|
|
|
FileUtil.copy(path + File.separator + userUploadExcel, source, true);
|
|
|
|
|
|
|
|
|
|
//解析上传EXCEL中的每个Sheet,解析出表头信息,表名描述等信息
|
|
|
|
|
InputStream is = new FileInputStream(source);
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
|
|
|
|
|
int sheetCount = wb.getNumberOfSheets();//Sheet表数量
|
|
|
|
|
List<Kv> kvList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < sheetCount; i++) {
|
|
|
|
|
try {
|
|
|
|
|
Kv kv = ExcelUtil.getTableStruct(wb, i);
|
|
|
|
|
if (!kv.getBoolean("success")) {
|
|
|
|
|
System.out.println("发现错误:" + kv.getStr("message"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
kv.set("upload_excel_filename", upload_excel_filename);
|
|
|
|
|
kvList.add(kv);
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
System.out.println("“" + wb.getSheetName(i) + "”表配置错误,请检查后重新上传!");
|
|
|
|
|
wb.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//提供给前台进行修改属性(现在这里是模拟的前端处理后的代码)
|
|
|
|
|
// (1)整体上需要添加一个参数:table_name
|
|
|
|
|
// (2)list中每个 Record 添加属性:column_type,allow_blank,在JAVA中,函数的参数是一个List<Kv>时,是按参数引用的,所以,直接修改即可
|
|
|
|
|
// (3)如果需要添加下拉框,需要设置下拉item的数据范围
|
|
|
|
|
changeData(kvList);
|
|
|
|
|
|
|
|
|
|
//开发时暴力删除掉原表,在正式上线前要注意:不能简单粗暴的删除!!
|
|
|
|
|
for (Kv kv : kvList) {
|
|
|
|
|
String tableName = kv.getStr("table_name");
|
|
|
|
|
ImportExcelKit.dropTable(tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断表是不是已存在
|
|
|
|
|
for (Kv kv : kvList) {
|
|
|
|
|
String tableName = kv.getStr("table_name");
|
|
|
|
|
if (ImportExcelKit.isTableExist(tableName)) {
|
|
|
|
|
System.out.println("表" + tableName + "已存在,不能删除,程序无法继续!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查通过,处理建表逻辑
|
|
|
|
|
for (Kv kv : kvList) {
|
|
|
|
|
String tableName = kv.getStr("table_name");
|
|
|
|
|
//ImportExcelKit.createTable(tableName, kv);//创建表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查是不是需要对指定列进行加红星+Comment操作
|
|
|
|
|
int sheetIdx = 0;
|
|
|
|
|
for (Kv kv : kvList) {
|
|
|
|
|
int start_row = kv.getInt("start_row");
|
|
|
|
|
int end_row = kv.getInt("end_row");
|
|
|
|
|
List<Record> list = (List<Record>) kv.get("list");
|
|
|
|
|
int colIdx = 0;
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
boolean allow_blank = record.getBoolean("allow_blank");
|
|
|
|
|
if (!allow_blank) {
|
|
|
|
|
//一行时,星号加在一行
|
|
|
|
|
//二行时,星号加在二行
|
|
|
|
|
XSSFCell cell = wb.getSheetAt(sheetIdx).getRow(end_row).getCell(colIdx);
|
|
|
|
|
if (StrKit.isBlank(cell.getStringCellValue())) {
|
|
|
|
|
cell = wb.getSheetAt(sheetIdx).getRow(start_row).getCell(colIdx);
|
|
|
|
|
}
|
|
|
|
|
ExcelUtil.addStar(wb, cell);
|
|
|
|
|
ExcelUtil.addComment(wb, cell, "此单元格内容必须输入!");
|
|
|
|
|
}
|
|
|
|
|
colIdx++;
|
|
|
|
|
}
|
|
|
|
|
sheetIdx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查是不是需要对于指定的Sheet+指定的列生成下拉框
|
|
|
|
|
sheetIdx = 0;
|
|
|
|
|
for (Kv kv : kvList) {
|
|
|
|
|
int data_start_row = kv.getInt("data_start_row");
|
|
|
|
|
List<Record> list = (List<Record>) kv.get("list");
|
|
|
|
|
int colIdx = 0;
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
String options = record.getStr("options");
|
|
|
|
|
if (!StrKit.isBlank(options)) {
|
|
|
|
|
ExcelUtil.addValidation(wb, sheetIdx, options, data_start_row, data_start_row + 20000, colIdx, colIdx);//范围
|
|
|
|
|
}
|
|
|
|
|
colIdx++;
|
|
|
|
|
}
|
|
|
|
|
sheetIdx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消每一个表单的激活状态
|
|
|
|
|
for (int i = 0; i < kvList.size(); i++) wb.getSheetAt(i).setSelected(false);
|
|
|
|
|
// 设置活动表单为第一个表单
|
|
|
|
|
wb.setActiveSheet(0);
|
|
|
|
|
|
|
|
|
|
//保存
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(source);
|
|
|
|
|
wb.write(fileOut);
|
|
|
|
|
//关闭Excel
|
|
|
|
|
wb.close();
|
|
|
|
|
//输出
|
|
|
|
|
System.out.println("恭喜,数据库表创建成功,共" + kvList.size() + "个。");
|
|
|
|
|
}
|
|
|
|
|
}
|