main
黄海 2 years ago
parent f7a79ba3fe
commit af6c3ef1b4

@ -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();
}
}

@ -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<Record> list = Db.find(sql, table_name);
sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
List<Record> list = Db.find(sql, upload_excel_filename, sheetIdx);
Map<Integer, Record> _map = new HashMap<>();
for (Record r : list) {
int excel_column_idx = r.getInt("excel_column_idx");
@ -387,25 +386,27 @@ public class ImportUtil {
}
//开始读取数据
List<Record> 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;
}
}
}

Loading…
Cancel
Save