main
黄海 2 years ago
parent e3720ad1d4
commit 6f97c7e2b8

@ -14,6 +14,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class ImportExcelData {
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
@ -38,19 +40,27 @@ public class ImportExcelData {
arp.start();
//模板文件
String source = path + File.separator + upload_excel_filename;
String f1 = path + File.separator + upload_excel_filename;
//用户上传的填充完的EXCEL文件
String schoolFile = path + File.separator + "school.xlsx";
String f2 = path + File.separator + "school.xlsx";
//对比两个EXCEL文件 是不是格式一致,也就是是不是上传了正确的模板文件
//GenericTemplateUtil.checkYiZhi(source,schoolFile,)
int sheetCnt = GenericTemplateUtil.getExcelSheetCount(upload_excel_filename);
for (int i = 0; i < sheetCnt; i++) {
List<Map.Entry<Integer, Integer>> chayi = GenericTemplateUtil.checkYiZhi(f1, f2, i);
if (chayi.size() > 0) {
System.out.println("第" + (i + 1) + "个Sheet表检查过程中发现与要求上传的模板不一致请重新下载模板填写完成后再次上传");
return;
}
}
System.out.println("OK,是好模板!");
// 检查是不是非空项目未填写
// 检查是不是下拉列表不是从下拉列表中选择的
// 检查数据类型是不是和规定的不兼容
//解析上传EXCEL中的每个Sheet解析出表头信息表名描述等信息
InputStream is = new FileInputStream(source);
XSSFWorkbook wb = new XSSFWorkbook(is);
// 全都通过,导入数据
//关闭POI的工作簿
wb.close();
}
}

@ -1,6 +1,7 @@
package com.dsideal.QingLong.Util;
import UnitTest.ImportExcel.Bean.CellBean;
import cn.hutool.core.io.FileUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
@ -99,19 +100,45 @@ public class GenericTemplateUtil {
return Db.findFirst(sql, tableName).getInt("c") == 1;
}
/**
* +Sheet
*
* @param upload_excel_filename
* @param sheet_index
* @return
*/
public static Record getExcelSheetInfo(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 getExcelSheetCount(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();
}
/**
* EXCEL+ Map.Entry<Integer, Integer>
*
* @param f1
* @param f2
* @param startRow
* @param endRow
* @param startCol
* @param endCol
* @param f1
* @param f2
* @return
* @throws IOException
*/
public static List<Map.Entry<Integer, Integer>> checkYiZhi(String f1, String f2, int sheetIdx, int startRow, int endRow, int startCol, int endCol) throws IOException {
public static List<Map.Entry<Integer, Integer>> checkYiZhi(String f1, String f2, int sheetIdx) throws IOException {
Record record = getExcelSheetInfo(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");
int end_column = record.getInt("end_column");
FileInputStream file1 = new FileInputStream(f1);
FileInputStream file2 = new FileInputStream(f2);
List<Map.Entry<Integer, Integer>> errList = new ArrayList<>();
@ -121,10 +148,10 @@ public class GenericTemplateUtil {
Sheet sheet1 = wb1.getSheetAt(sheetIdx);
Sheet sheet2 = wb2.getSheetAt(sheetIdx);
for (int i = startRow; i <= endRow; i++) {
for (int i = start_row; i <= end_row; i++) {
Row row1 = sheet1.getRow(i);
Row row2 = sheet2.getRow(i);
for (int j = startCol; j <= endCol; j++) {
for (int j = start_column; j <= end_column; j++) {
Cell cell1 = row1.getCell(j);
Cell cell2 = row2.getCell(j);
if (cell1 != null && cell2 != null) {
@ -190,7 +217,7 @@ public class GenericTemplateUtil {
kv.set("start_row", st.getRowNum());//表头开始行
kv.set("end_row", ed.getRowNum());//表头结束行
kv.set("start_column", 0);//表头开始列
kv.set("end_column", ed.getLastCellNum());//表头结束列
kv.set("end_column", ed.getLastCellNum() - 1);//表头结束列
kv.set("data_start_row", ed.getRowNum() + 1);//真实数据的起始行索引
kv.set("column_num", ed.getLastCellNum());//一共多少列

Loading…
Cancel
Save