You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
8.0 KiB

2 years ago
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.AsposeUtil;
2 years ago
import com.dsideal.QingLong.Util.ImportUtil;
2 years ago
import com.jfinal.kit.PropKit;
2 years ago
import com.jfinal.kit.StrKit;
2 years ago
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
2 years ago
import com.jfinal.plugin.activerecord.Record;
2 years ago
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
2 years ago
import org.apache.poi.openxml4j.util.ZipSecureFile;
2 years ago
import org.apache.poi.ss.usermodel.IndexedColors;
2 years ago
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2 years ago
2 years ago
import java.io.*;
2 years ago
import java.text.ParseException;
2 years ago
import java.util.List;
import java.util.Map;
2 years ago
import java.util.UUID;
2 years ago
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";
2 years ago
public static String bureau_id = "1fb8611d-2bf3-4a82-a7e8-b03206338deb";
public static String person_id = "c8ef14f2-5dcc-4b62-8964-2282b68ba21d";
2 years ago
2 years ago
public static void main(String[] args) throws IOException, ParseException {
2 years ago
//加载License
AsposeUtil.getLicense();
2 years ago
// 延迟解析比率
ZipSecureFile.setMinInflateRatio(-1.0d);
2 years ago
//告之配置文件位置
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();
2 years ago
//模板文件
2 years ago
String f1 = path + File.separator + upload_excel_filename;
2 years ago
//用户上传的填充完的EXCEL文件
2 years ago
String f2 = path + File.separator + "school.xlsx";
2 years ago
//对比两个EXCEL文件 是不是格式一致,也就是是不是上传了正确的模板文件
2 years ago
int sheetCnt = ImportUtil.getExcelSheetCount(upload_excel_filename);
2 years ago
for (int i = 0; i < sheetCnt; i++) {
2 years ago
List<Map.Entry<Integer, Integer>> chayi = ImportUtil.checkYiZhi(f1, f2, i);
2 years ago
if (chayi.size() > 0) {
System.out.println("第" + (i + 1) + "个Sheet表检查过程中发现与要求上传的模板不一致请重新下载模板填写完成后再次上传");
return;
}
}
2 years ago
System.out.println("OK,是我下发的模板!");
2 years ago
2 years ago
//打开EXCEL进行检查
2 years ago
2 years ago
InputStream is = new FileInputStream(f2);
XSSFWorkbook wb = new XSSFWorkbook(is);
//遍历每个Sheet注册好的信息对用户上传的数据表进行检查
2 years ago
boolean flag = true;
2 years ago
for (int i = 0; i < sheetCnt; i++) {//表
//Sheet表
XSSFSheet sheet = wb.getSheetAt(i);
2 years ago
//移除所有批注
ImportUtil.RemoveAllComment(sheet);
2 years ago
//数据起始行
Record r = ImportUtil.getExcelSheetInfo(upload_excel_filename, i);
int data_start_row = r.getInt("data_start_row");
2 years ago
//恢复背景色
ImportUtil.resetCellStyle(wb, sheet, data_start_row);
2 years ago
//数据有效行数
int lastRowNum = sheet.getLastRowNum();
//遍历每一列
2 years ago
List<Record> list = ImportUtil.getExcelSheetColumnsInfo(upload_excel_filename, i);
2 years ago
for (Record record : list) {//列
int excel_column_idx = record.getInt("excel_column_idx");//第几列
String column_type = record.getStr("column_type");//类型
2 years ago
String options = record.getStr("options");//下拉框内容
2 years ago
boolean allow_blank = record.getBoolean("allow_blank");//是不是允许为空
2 years ago
2 years ago
if (!allow_blank) {
// 非空项目未填写
for (int j = data_start_row; j <= lastRowNum; j++) {//行
2 years ago
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
2 years ago
if (StrKit.isBlank(ImportUtil.getCellValue(cell).toString())) {
ImportUtil.addComment(wb, cell, "此处不能为空!");
2 years ago
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
2 years ago
flag = false;
2 years ago
}
}
}
// 下拉列表不是从下拉列表中选择的
2 years ago
if (!StrKit.isBlank(options)) {
for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
String value = ImportUtil.getCellValue(cell).toString();
if (StrKit.isBlank(value)) value = "&*^&Y&*(&*(&*()*(";
if (options.indexOf(value) < 0) {
ImportUtil.addComment(wb, cell, "未从指定范围内选择有效值!");
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
}
}
}
2 years ago
// 检查数据类型是不是和规定的不兼容
2 years ago
for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
Object obj = ImportUtil.getCellValue(cell);
2 years ago
if (column_type.equals("Integer") && !ImportUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
2 years ago
ImportUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!");
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
2 years ago
} else if (column_type.equals("Double") && !ImportUtil.isDouble(obj.toString())) {//要求是浮点数,实际不是浮点数
2 years ago
ImportUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!");
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
2 years ago
} else if (column_type.equals("Date") && !ImportUtil.isDate(obj.toString())) {//要求是日期,实际不是日期
2 years ago
ImportUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!");
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
}
}
2 years ago
}
}
2 years ago
2 years ago
// 取消每一个表单的激活状态
for (int i = 0; i < sheetCnt; i++) wb.getSheetAt(i).setSelected(false);
// 设置活动表单为第一个表单
wb.setActiveSheet(0);
//保存文件
FileOutputStream fileOut = new FileOutputStream(f2);
wb.write(fileOut);
if (!flag) {
System.out.println("检查到输入的文件中存在不合法情况,请下载文件后查看处理后,重新上传!");
2 years ago
//关闭Excel
wb.close();
2 years ago
return;
}
2 years ago
2 years ago
// 检查通过,导入数据
System.out.println("检查通过,可以导入数据!");
2 years ago
for (int i = 0; i < sheetCnt; i++) ImportUtil.importData(upload_excel_filename, wb, i, bureau_id, person_id);
2 years ago
System.out.println("恭喜,所有数据成功导入!");
//关闭Excel
wb.close();
2 years ago
}
}