main
黄海 2 years ago
parent 9eaf0b1cf5
commit a1bc676c8d

@ -52,7 +52,7 @@ public class ImportExcelData {
//用户上传的填充完的EXCEL文件
String f2 = path + File.separator + "school.xlsx";
//对比两个EXCEL文件 是不是格式一致,也就是是不是上传了正确的模板文件
int sheetCnt = ImportUtil.getExcelSheetCount(upload_excel_filename);
int sheetCnt = ImportUtil.getSheetCount(upload_excel_filename);
for (int i = 0; i < sheetCnt; i++) {
List<Map.Entry<Integer, Integer>> chayi = ImportUtil.checkYiZhi(f1, f2, i);
if (chayi.size() > 0) {
@ -74,15 +74,15 @@ public class ImportExcelData {
ImportUtil.RemoveAllComment(sheet);
//数据起始行
Record r = ImportUtil.getExcelSheetInfo(upload_excel_filename, i);
Record r = ImportUtil.getSheetConfig(upload_excel_filename, i);
int data_start_row = r.getInt("data_start_row");
//恢复背景色
ImportUtil.resetCellStyle(wb, sheet, data_start_row);
ImportUtil.resetStyle(wb, sheet, data_start_row);
//数据有效行数
int lastRowNum = sheet.getLastRowNum();
//遍历每一列
List<Record> list = ImportUtil.getExcelSheetColumnsInfo(upload_excel_filename, i);
List<Record> list = ImportUtil.getSheetMapping(upload_excel_filename, i);
for (Record record : list) {//列
int excel_column_idx = record.getInt("excel_column_idx");//第几列
String column_type = record.getStr("column_type");//类型
@ -93,7 +93,7 @@ public class ImportExcelData {
// 非空项目未填写
for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
if (StrKit.isBlank(ImportUtil.getCellValue(cell).toString())) {
if (StrKit.isBlank(ImportUtil.getValue(cell).toString())) {
ImportUtil.addComment(wb, cell, "此处不能为空!");
ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
@ -104,7 +104,7 @@ public class ImportExcelData {
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();
String value = ImportUtil.getValue(cell).toString();
if (StrKit.isBlank(value)) value = "&*^&Y&*(&*(&*()*(";
if (options.indexOf(value) < 0) {
ImportUtil.addComment(wb, cell, "未从指定范围内选择有效值!");
@ -116,7 +116,7 @@ public class ImportExcelData {
// 检查数据类型是不是和规定的不兼容
for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
Object obj = ImportUtil.getCellValue(cell);
Object obj = ImportUtil.getValue(cell);
if (column_type.equals("Integer") && !ImportUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
ImportUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!");

@ -21,12 +21,12 @@ public class ImportUtil {
/**
*
*
* @param input
* @param str
* @return
*/
public static String removeKuoHao(String input) {
input = input.replace("\n", "");
String output = input.replaceAll("\\(.*?\\)", "");
public static String removeKuoHao(String str) {
str = str.replace("\n", "");
String output = str.replaceAll("\\(.*?\\)", "");
output = output.replaceAll("\\.*?\\", "");
return output;
}
@ -38,7 +38,7 @@ public class ImportUtil {
* @param cell
* @return
*/
private static String getCellColor(Cell cell) {
private static String getColor(XSSFCell cell) {
if (cell != null) {
//背景颜色
CellStyle cellStyle = cell.getCellStyle();
@ -64,15 +64,15 @@ public class ImportUtil {
// 遍历行
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
//获得行
Row row = sheet.getRow(i);
XSSFRow 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);
XSSFCell cell = row.getCell(j);
if (cell == null) continue;
String color = getCellColor(cell);
String color = getColor(cell);
//记录背景颜色数量
if (_map.containsKey(color))
_map.put(color, _map.get(color) + 1);
@ -105,7 +105,7 @@ public class ImportUtil {
* @param sheet_index
* @return
*/
public static Record getExcelSheetInfo(String upload_excel_filename, int sheet_index) {
public static Record getSheetConfig(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);
}
@ -116,7 +116,7 @@ public class ImportUtil {
* @param upload_excel_filename
* @return
*/
public static int getExcelSheetCount(String upload_excel_filename) {
public static int getSheetCount(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();
@ -129,7 +129,7 @@ public class ImportUtil {
* @param sheet_index
* @return
*/
public static List<Record> getExcelSheetColumnsInfo(String upload_excel_filename, int sheet_index) {
public static List<Record> getSheetMapping(String upload_excel_filename, int sheet_index) {
String sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
return Db.find(sql, upload_excel_filename, sheet_index);
}
@ -143,7 +143,7 @@ public class ImportUtil {
* @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);
Record record = getSheetConfig(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");
@ -401,13 +401,13 @@ public class ImportUtil {
String colType = _map.get(j).getStr("column_type");
String colName = _map.get(j).getStr("column_name");
if (colType.equals("Integer"))
writeRecord.set(colName, (int) Double.parseDouble(getCellValue(cell).toString()));
writeRecord.set(colName, (int) Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("String"))
writeRecord.set(colName, getCellValue(cell).toString());
writeRecord.set(colName, getValue(cell).toString());
else if (colType.equals("Double"))
writeRecord.set(colName, Double.parseDouble(getCellValue(cell).toString()));
writeRecord.set(colName, Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("Date")) {
String dateString = getCellValue(cell).toString();
String dateString = getValue(cell).toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);
writeRecord.set(colName, date);
@ -508,7 +508,7 @@ public class ImportUtil {
* @param wb
* @param sheet
*/
public static void resetCellStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) {
public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) {
// 遍历所有行和单元格,移除批注
for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) {
for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) {
@ -583,8 +583,7 @@ public class ImportUtil {
* @param cell
* @return
*/
public static Object getCellValue(XSSFCell cell) {
public static Object getValue(XSSFCell cell) {
switch (cell.getCellType()) {
case CellType.STRING:
String cellValueString = cell.getStringCellValue();

Loading…
Cancel
Save