批量导入订单信息
1.jsp
//导入雇员基本信息
function importContractEmployees(){
debugger;
var source = "batchEmployee";
var fileInput = $("#fileId").val(); //附件input
if (fileInput == "" || fileInput == null) {
layerCommon.layerAlert("请导入填写后的模板文件!");
return false;
}else{
if("batchEmployee"==source){
//$("body").showLoading();//遮罩层打开
$.ajaxFileUpload({
url : ctx+'/ord/batch/ordBatch/uploadExcel',
secureuri : false,
fileElementId : ['fileId'],
dataType : 'json',
data : {},
success : function(data, status) {
//$("body").hideLoading();//遮罩层关闭
if (data.state == "1") {
$("#step1").hide();
$("#step2").show();
$(".z-index2").addClass("green");
} else {
//错误信息
layerCommon.layerAlert(data.msg);
}
},
error : function(data, status, e) {
layerCommon.layerAlert(e);
$("body").hideLoading();//遮罩层关闭
}
})
}
}
}
2.后台 Controller
@RequestMapping("uploadExcel")
@Async
@ResponseBody
public OrdBatchStatusModel<String> uploadExcel(HttpServletRequest request) throws IOException {
User user = SessionUtil.getLoginUser();
String isEntrystr = request.getParameter("isEntry");
OrdBatchStatusModel<String> result = null;
List<InputStream> filesList = FileUtils.uploadExcel(request);
if (filesList.size()==1){
Boolean isEntry = true;
if("false".equals(isEntrystr)) {
isEntry = false;
}
logger.info(" 导入员工基本信息Controller begin: " + DateUtils.getDate("yyyyMMddHHmmss"));
try {
result = ordBatchService.batchOrderSave(user,filesList.get(0),isEntry);
} catch (Exception e) {
e.printStackTrace();
}
logger.info(" 导入员工服务Controller end: " + DateUtils.getDate("yyyyMMddHHmmss"));
}else if(filesList==null || filesList.size()!=1) {
throw new FescoException("只能上传一个文件.发现"+filesList.size()+"个文件.");
}
else {
throw new FescoException("缺少必要文件.");
}
return result;
}
3.后台Service
@Transactional
public OrdBatchStatusModel<String> batchOrderSave(User user, InputStream inputStream,Boolean isEntry) throws Exception {
OrdBatchStatusModel<String> commonResultBean = new OrdBatchStatusModel<>("1", "导入成功");
String _ThreadName = Thread.currentThread().getName();
String orgId=SessionUtil.getUserOfficeId();
Map<String, ContarctEmpModel> contractMap = new HashMap<>();// 用来合同分组
try {
HSSFWorkbook wb = new HSSFWorkbook(new BufferedInputStream(inputStream));
String version = ExcelUtils.checkVersion(wb,ExcelUtils.BATCH_EMPLOYEE_INFO_VERSION);
if(!"true".equals(version)) {
ExceptionUtil.handle2Exception("版本("+version+")与服务器版本("+ExcelUtils.BATCH_EMPLOYEE_INFO_VERSION+")不一致");
}
HSSFSheet sheet = wb.getSheet("雇员信息模板");
Map<String, Integer> resMap = ExcelUtils.getSecondMap(sheet);
// 得到总行数 //得到总列数 int colNum = row.getPhysicalNumberOfCells();
int rowNum = sheet.getPhysicalNumberOfRows();
if(rowNum>=1500) {
// 强制设置成1500行
rowNum = 1500;
}
// 校验数据正确性
List<String> errorDataList = checkData(wb,sheet, resMap, rowNum);
if (errorDataList.size() > 0) {
commonResultBean.setState("3");
commonResultBean.setMsg(errorDataList.get(0));
commonResultBean.setList(errorDataList);
inputStream.close();
return commonResultBean;
}
/** ---------根据用户选择合同数,创建对应的list,每个合同下的客户封装到不同list里 */
for (int i = 2; i < rowNum; i++) {
HSSFRow row = sheet.getRow(i);
String idAndName = row.getCell(resMap.get("contract_name")).getStringCellValue();
String conId=ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,0,idAndName);
if (!"".equals(conId) && contractMap.get(conId) == null) {
ContarctEmpModel batchEmpInfoModel = new ContarctEmpModel();
batchEmpInfoModel.setContractId(conId);
List<ContarctEmpModel> infoList = new ArrayList<ContarctEmpModel>();
batchEmpInfoModel.setConEmpModelList(infoList);
contractMap.put(conId, batchEmpInfoModel);
}
}
String cellValue = null;
/** ---------循环封装数据到map里对应的list中--------开始 */
for (int i = 2; i < rowNum; i++) {
HSSFRow row = sheet.getRow(i);
String idAndName = row.getCell(resMap.get("contract_name")).getStringCellValue();
String conCode=ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,0,idAndName);
if (!"".equals(conCode)) {
ContarctEmpModel bean = new ContarctEmpModel();
bean.setContractId(conCode);
bean.setEmpName(row.getCell(resMap.get("employee_name")).getStringCellValue());// 雇员姓名
if(orgId==null){
//职业
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("job"));
bean.setIdNum(cellValue);
//岗位职称
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("skillState"));
bean.setIdNum(cellValue);
//用工形式
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("useFormat"));
bean.setIdNum(cellValue);
}
// 证件类型
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_certificate_type"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setIdType(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_certificate_type"), cellValue));
}else{
bean.setIdType(employeeCertificateType);
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_certificate_number"));
cellValue = cellValue.replace("x", "X");// 身份证号
bean.setIdNum(cellValue.trim());// 身份证号
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_moblie"));
bean.setEmpPhone(cellValue);// 手机
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("ce_bank_name"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setBankAccount(DictUtil.getDictValue(cellValue, "bank_account", ""));// 工资卡银行
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("ce_bank_card_num"));
bean.setEmpCardNum(cellValue);// 银行卡号
//员工国籍
cellValue =ExcelUtils.getValueFormRow(row, resMap.get("country_type"));
if (StringUtils.isNotBlank(cellValue)) {
if(DictUtil.getDictValue(cellValue, "country", "").equals("china")){
bean.setCountry("china");
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_province"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setProId(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_residence_province"), cellValue));
//bean.setProId(cellValue);// 省份
}
cellValue =ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_city"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setCityId(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_residence_city"), cellValue));// 城市
}
cellValue =ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_area"));
if (StringUtils.isNotBlank(cellValue)) {
String areaId=empInfoService.getAreaId(cellValue);
bean.setAreaId(areaId);// 区
}
}else{
//bean.setCountry(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("country_type"), cellValue));
bean.setCountry(DictUtil.getDictValue(cellValue, "country", ""));
}
}else{
bean.setCountry("china");
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_province"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setProId(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_residence_province"), cellValue));
//bean.setProId(cellValue);// 省份
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_city"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setCityId(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_residence_city"), cellValue));// 城市
//bean.setCityId(cellValue);// 城市
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_residence_area"));
if (StringUtils.isNotBlank(cellValue)) {
String areaId=empInfoService.getAreaId(cellValue);
bean.setAreaId(areaId);// 区
}
}
// Date
// employeeFirstWorkAtDate=dateFormat.parse(row.getCell(resMap.get("employee_first_work_at")).getStringCellValue());
String employee_first_work_at = ExcelUtils.getValueFormRow(row, resMap.get("employee_first_work_at"));
String employee_entry_at = ExcelUtils.getValueFormRow(row, resMap.get("employee_entry_at"));
if (StringUtils.isBlank(employee_entry_at)) {
inputStream.close();
commonResultBean.setState("2");
commonResultBean.setMsg("时间格式不正确");
return commonResultBean;
}
if (StringUtils.isBlank(employee_first_work_at)) {
inputStream.close();
commonResultBean.setState("2");
commonResultBean.setMsg("时间格式不正确");
return commonResultBean;
}
bean.setFirstWorkDate(employee_first_work_at);// 第一次参加工作日期
bean.setEntryDate(employee_entry_at);// 入职现公司日期
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_address"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setEmpResidence(cellValue);// 居住地
}
String employee_degree = ExcelUtils.getValueFormRow(row, resMap.get("employee_degree"));
if (row.getCell(resMap.get("employee_degree")) != null) {
bean.setDegree(ExcelUtils.readHideSheetDate(wb, "hideDateSheet", i,resMap.get("employee_degree"), employee_degree));// 学历
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("employee_internal_number"));
if (StringUtils.isNotBlank(cellValue)) {
bean.setIntNum(cellValue);// 内部编号
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("si_per_account"));
if (StringUtils.isNotBlank(cellValue)) {
if (cellValue.length() != 8) {
commonResultBean.setState("2");
commonResultBean.setMsg("养老编号格式不正确(应为8位纯数字)");
return commonResultBean;
} else {
bean.setPensionNum(cellValue);// 养老编号
}
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("si_per_maccount"));
if (StringUtils.isNotBlank(cellValue)) {
if (cellValue.length() != 10) {
commonResultBean.setState("2");
commonResultBean.setMsg("医疗编号格式不正确(应为10位纯数字)");
return commonResultBean;
} else {
bean.setMedicalNum(cellValue);// 医疗编号
}
}
cellValue = ExcelUtils.getValueFormRow(row, resMap.get("hf_per_account"));
if (StringUtils.isNotBlank(cellValue)) {
if (cellValue.length() != 12) {
commonResultBean.setState("2");
commonResultBean.setMsg("公积金编号格式不正确(应为12位纯数字)");
return commonResultBean;
} else {
bean.setaFundNum(cellValue);// 公积金编号
}
}
//如果是身份证就计算性别
if(bean.getIdType().equals("1")){
String value = ExcelUtils.getValueFormRow(row, resMap.get("employee_certificate_number"));
cellValue=value.trim();
String month=null;
String day=null;
if(IdcardUtils.getMonthByIdCard(cellValue)<10){
month="0"+IdcardUtils.getMonthByIdCard(cellValue);
}else{
month=IdcardUtils.getMonthByIdCard(cellValue).toString();
}
if(IdcardUtils.getDateByIdCard(cellValue)<10){
day="0"+IdcardUtils.getDateByIdCard(cellValue);
}else{
day=IdcardUtils.getDateByIdCard(cellValue).toString();
}
bean.setBirthday(IdcardUtils.getYearByIdCard(cellValue)+"-"+month+"-"+day);
bean.setBirthRest((IdcardUtils.getGenderByIdCard(cellValue)).equals("M") ? "1" : "2");
}else{
bean.setBirthday("1970-01-01");
bean.setBirthRest("0");
}
// 组装bean完成,根据合同号放入对应list中
contractMap.get(conCode).getConEmpModelList().add(bean);
}
/** ---------循环封装数据到map里对应的list中--------结束 */
}
for (Entry<String, ContarctEmpModel> entry : contractMap.entrySet()) {
for(int i=0;i<entry.getValue().getConEmpModelList().size();i++){
//System.out.println("循环list:"+entry.getValue().getConEmpModelList().get(i));
entry.getValue().getConEmpModelList().get(i);
empInfoService.conEmpSave(entry.getValue().getConEmpModelList().get(i),"nor_order","1");
}
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
commonResultBean.setState("2");
commonResultBean.setMsg("导入雇员基本信息出错"+ExceptionUtil.printException(e));
}
//System.out.println(_ThreadName + " end: " + new Date());
return commonResultBean;
}
.
4.后台Util(因为在隐藏的sheet页中是按照value,key 格式隐藏的,需要通过value拿key)
/**
* 根据字符串读取字符串ID(合同,1)
* @param workbook
* @param hideSheetName
* @param rowNum
* @param celNum
* @param strKey
* @return ID
*/
public static String readHideSheetDate(HSSFWorkbook workbook, String hideSheetName,int rowNum, int celNum,String strKey) {
String resId = "";
HSSFSheet hideSheet = null;
if(workbook.getSheet(hideSheetName) == null){
return "";
}else{
hideSheet = workbook.getSheet(hideSheetName);
}
HSSFRow row = hideSheet.getRow(rowNum);
HSSFCell cel = row.getCell(celNum);
String value = cel.getStringCellValue();// 合同A,1;合同B,2;合同C,3;
System.out.println("获取到的value:"+value);
String[] values = value.split(";");
for (int i = 0; i < values.length; i++) {
String str = values[i];
String[] strs = str.split(",");
if(strKey.equals(strs[0])){
resId = strs[1];
break;
}
}
return resId;
}