diff --git a/src/main/java/UnitTest/ImportExcel/ImportExcelData.java b/src/main/java/UnitTest/ImportExcel/ImportExcelData.java index 11f3f042..e6c6e6ac 100644 --- a/src/main/java/UnitTest/ImportExcel/ImportExcelData.java +++ b/src/main/java/UnitTest/ImportExcel/ImportExcelData.java @@ -15,9 +15,10 @@ import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; -import java.util.Date; +import java.text.ParseException; import java.util.List; import java.util.Map; +import java.util.UUID; public class ImportExcelData { public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\"; @@ -25,8 +26,10 @@ public class ImportExcelData { //模拟上传文件的文件名称 public static String upload_excel_filename = "b61a2af2-223f-4058-a675-b212e4dd9487" + ".xlsx"; + public static String bureau_id = UUID.randomUUID().toString().toLowerCase(); + public static String person_id = UUID.randomUUID().toString().toLowerCase(); - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, ParseException { //加载License AsposeUtil.getLicense(); @@ -113,19 +116,16 @@ 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); - if (column_type.equals("String") && !(obj instanceof String)) {//要求字符串,实际不是字符串 - ImportUtil.addComment(wb, cell, "要求是字符串,实际数据类型不是字符串!"); - ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); - flag = false; - } else if (column_type.equals("Integer") && !(obj instanceof Integer)) {//要求整数,实际不是整数 + + if (column_type.equals("Integer") && !ImportUtil.isInteger(obj.toString())) {//要求整数,实际不是整数 ImportUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!"); ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; - } else if (column_type.equals("Double") && !(obj instanceof Double)) {//要求是浮点数,实际不是浮点数 + } else if (column_type.equals("Double") && !ImportUtil.isDouble(obj.toString())) {//要求是浮点数,实际不是浮点数 ImportUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!"); ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; - } else if (column_type.equals("Date") && !(obj instanceof Date)) {//要求是日期,实际不是日期 + } else if (column_type.equals("Date") && !ImportUtil.isDate(obj.toString())) {//要求是日期,实际不是日期 ImportUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!"); ImportUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; @@ -141,14 +141,20 @@ public class ImportExcelData { //保存文件 FileOutputStream fileOut = new FileOutputStream(f2); wb.write(fileOut); - //关闭Excel - wb.close(); if (!flag) { System.out.println("检查到输入的文件中存在不合法情况,请下载文件后查看处理后,重新上传!"); + //关闭Excel + wb.close(); return; } - // 全都通过,导入数据 + // 检查通过,导入数据 + System.out.println("检查通过,可以导入数据!"); + for (int i = 0; i < sheetCnt; i++) ImportUtil.importData(upload_excel_filename, wb, i,bureau_id,person_id); + + System.out.println("恭喜,所有数据成功导入!"); + //关闭Excel + wb.close(); } } diff --git a/src/main/java/com/dsideal/QingLong/Util/ImportUtil.java b/src/main/java/com/dsideal/QingLong/Util/ImportUtil.java index d083f780..999f47a1 100644 --- a/src/main/java/com/dsideal/QingLong/Util/ImportUtil.java +++ b/src/main/java/com/dsideal/QingLong/Util/ImportUtil.java @@ -357,24 +357,23 @@ public class ImportUtil { /** * 功能:导入数据 * - * @param table_name - * @param filePath * @throws IOException * @throws ParseException */ - public static void importData(String table_name, String filePath) throws IOException, ParseException { - InputStream is = new FileInputStream(filePath); - XSSFWorkbook wb = new XSSFWorkbook(is); + public static void importData(String upload_excel_filename, XSSFWorkbook wb, int sheetIdx, String bureau_id, String person_id) throws IOException, ParseException { //读取sheet页 - XSSFSheet sheet = wb.getSheetAt(0); + XSSFSheet sheet = wb.getSheetAt(sheetIdx); //通过表名获取到它的读取起始行,终止列 - String sql = "select * from t_importexcel_config where table_name=?"; - Record record = Db.findFirst(sql, table_name); + String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?"; + Record record = Db.findFirst(sql, upload_excel_filename, sheetIdx); + String table_name = record.getStr("table_name"); + int data_start_row = record.getInt("data_start_row"); int column_num = record.getInt("column_num"); + //获取字段与EXCEL列的映射信息 - sql = "select * from t_importexcel_mapping where table_name=?"; - List list = Db.find(sql, table_name); + sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?"; + List list = Db.find(sql, upload_excel_filename, sheetIdx); Map _map = new HashMap<>(); for (Record r : list) { int excel_column_idx = r.getInt("excel_column_idx"); @@ -387,25 +386,27 @@ public class ImportUtil { } //开始读取数据 List writeList = new ArrayList<>(); - for (int i = data_start_row - 1; i <= sheet.getLastRowNum(); i++) { + for (int i = data_start_row; i <= sheet.getLastRowNum(); i++) { //获得行 - Row row = sheet.getRow(i); + XSSFRow row = sheet.getRow(i); + //遍历列 if (row != null) { Record writeRecord = new Record(); + writeRecord.set("bureau_id", bureau_id); + writeRecord.set("person_id", person_id); for (int j = 0; j < column_num; j++) { - Cell cell = row.getCell(j); - CellBean bean = getCellBean(cell); - String colType = _map.get(j + 1).getStr("column_type"); - String colName = _map.get(j + 1).getStr("column_name"); - if (colType.equals("int")) - writeRecord.set(colName, Integer.parseInt(bean.getValue())); - else if (colType.equals("varchar")) - writeRecord.set(colName, bean.getValue()); - else if (colType.equals("float")) - writeRecord.set(colName, Float.parseFloat(bean.getValue().toString())); - else if (colType.equals("date")) { - String dateString = bean.getValue(); + XSSFCell cell = row.getCell(j); + 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())); + else if (colType.equals("String")) + writeRecord.set(colName, getCellValue(cell).toString()); + else if (colType.equals("Double")) + writeRecord.set(colName, Double.parseDouble(getCellValue(cell).toString())); + else if (colType.equals("Date")) { + String dateString = getCellValue(cell).toString(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(dateString); writeRecord.set(colName, date); @@ -414,10 +415,9 @@ public class ImportUtil { writeList.add(writeRecord); } } + System.out.println("写入数据" + writeList.size() + "条!"); // 写入数据 Db.batchSave(table_name, writeList, 100); - //关闭excel - wb.close(); } @@ -611,4 +611,51 @@ public class ImportUtil { return cellValueString; } } + + /** + * 功能:判断字符串是不是能转换为整数 + * + * @param str + * @return + */ + public static boolean isInteger(String str) { + try { + double doubleValue = Double.parseDouble(str); + int intValue = (int) doubleValue; + return true; + } catch (NumberFormatException e) { + return false; + } + } + + /** + * 功能:判断字符串是不是能转换为浮点数 + * + * @param str + * @return + */ + public static boolean isDouble(String str) { + try { + Double.parseDouble(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + /** + * 功能:判断字符串是不是能转换为日期 + * + * @param str + * @return + */ + public static boolean isDate(String str) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + try { + sdf.parse(str); + return true; + } catch (ParseException e) { + return false; + } + } } diff --git a/src/main/resource/Excel/school.xlsx b/src/main/resource/Excel/school.xlsx index 68d624f8..a269529e 100644 Binary files a/src/main/resource/Excel/school.xlsx and b/src/main/resource/Excel/school.xlsx differ