导入同一个excel的多个工作表,并分多次批量插入到数据库

效果:

excel内容如下:

 

 

插入数据库效果如下:

表一:插入sheet1

导入同一个excel的多个工作表,并分多次批量插入到数据库_第1张图片

 

表二:插入sheet2

 

 

上代码:

jsp:

<%----%>
数据上传及更新 下载模板
日志信息

【导入数据处理】导入格式为XLSX *请使用模板导入数据,否则可能出现错误。请勿重复点击下载模板!

js:

//导入  数据上传及更新
function newImportDate() {
	debugger;
	var fileName= $('#newUploadFile').filebox('getValue');
	if(fileName==""){
		$.messager.alert('提示', '请选择上传文件!', 'info');
	}else if(!isFileVelidate(fileName,['xlsx','xls'])){
		$.messager.alert('提示', '请选择"xls"或"xlsx"文件!', 'info');
	}else{
		onloading();//开启加载效果
		$('#supCodefm').form('submit', {
			url : rootPath+'a/basicinfo/GSPM026/newImportDate' ,
			type : 'post',
			dataType: 'json',
			data:$('#supCodefm').serialize(),
			success : function(data) {
				var result = eval('(' + data + ')');
				if(result.success){
					$("#newUploadFile").filebox('clear');
					$.messager.alert('提示信息', result.messageContent, 'info');
					$('#dd').dialog('close');
					$('#supCodefm').form('clear');
					removeload();// 关闭加载效果
//				doSearch();
				}else{
					$('#supCodefm').form('clear');
					$.messager.alert('提示信息', result.messageContent, 'error');
					removeload();// 关闭加载效果
					$('#newShowMsg').linkbutton({
						disabled : false
					});

				}
				$('#supCodedlg').dialog('close')//关闭页面
				$('#tt').datagrid('reload');
				sendType='2';
				reset();
				doSearch();
				// refreshs();//刷新页面
			},
			error : function(data) {
				$.messager.alert('提示信息','系统出错','warning');
				$('#supCodefm').form('clear');
			}
		});
	}
}

controller:

/**
	 * 导入数据处理
	 * @param file
	 * @param redirectAttributes
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "newImportDate", method=RequestMethod.POST)
	@ResponseBody
	public String newImportDate(MultipartFile file, RedirectAttributes redirectAttributes) throws Exception {
		MessageInfo msgInfo = new MessageInfo();//消息类
		try {

			//导入第一个工作表
			gSPM026Service.importSheetNo1(file);
			//导入第二个工作表
			gSPM026Service.importSheetNo2(file);
			//更新数据
			gSPM026Service.updateData();

			msgInfo.setSuccess(true);
			msgInfo.setMessageContent("导入完了!");
		} catch (BusinessException ex) {
			msgInfo.setMessageContent(ex.getMessage());
			msgInfo.setSuccess(false);
			this.logUtilService.writeLog("GSPM026-构成品番信息维护-导入", msgInfo);
		} catch (Exception ex){
			msgInfo.setMessageContent(ex.getMessage());
			msgInfo.setSuccess(false);
			this.logUtilService.writeLog("GSPM026-构成品番信息维护-导入", msgInfo);
		}
		return JSONObject.fromObject(msgInfo).toString();
	}

service:

/**
	 * 导入第一个工作表
	 * @param file
	 * @throws Exception
	 */
	public void importSheetNo1(MultipartFile file) throws Exception {
		MessageInfo msgInfo = new MessageInfo();//消息类
		InputStream inputStream = null;
		XSSFWorkbook workbook = null; //1、定义工作簿
		//判断文件是否上传
		try {
			inputStream = file.getInputStream();
			//解析Xls xlsx文件
			if (inputStream != null) {
				workbook = new XSSFWorkbook(inputStream);
				//3、获取第一个工作表
				Sheet sheet = workbook.getSheetAt(0);
				if (sheet == null) {
					msgInfo.setSuccess(false);
					msgInfo.setMessageContent("上传Excel文件中不存在工作表,请检查!");
					throw new Exception("上传Excel文件中不存在工作表,请检查!");
				}
				//4、判断是否有数据
				//获取表格中最后一行的行号
				int lastRowNum = sheet.getLastRowNum();
				if (lastRowNum < 2) {//0行是说明 1行是标题
					msgInfo.setSuccess(false);
					msgInfo.setMessageContent("请录入数据!");
					throw new Exception("请录入数据!");
				}

				//得到用户登录名
				String username = UserUtils.getUser().getName();
				//5、循环读取数据
				List list = new ArrayList();
				List strErrorInfo = new ArrayList<>();//存放转换过程中的错误信息
				for (int i = 1; i <= lastRowNum; i++) {
					Row thisRow = sheet.getRow(i);//读取行
					if (thisRow != null) {
						//读取列
						String NO = thisRow.getCell(0) == null ? "" : thisRow.getCell(0).toString();
						String partsno = thisRow.getCell(1) == null ? "" : thisRow.getCell(1).toString();
						String inoutflag = thisRow.getCell(2) == null ? "" : thisRow.getCell(2).toString();
						String carfamilycode = thisRow.getCell(3) == null ? "" : thisRow.getCell(3).toString();
						String oesp = thisRow.getCell(4) == null ? "" : thisRow.getCell(4).toString();
						String inclusionpart = thisRow.getCell(5) == null ? "" : thisRow.getCell(5).toString();
						String isok = thisRow.getCell(6) == null ? "" : thisRow.getCell(6).toString();

						SpmPartsAssembly2Entity entity = new SpmPartsAssembly2Entity();
						if (!"补给品番".equals(partsno) && !"".equals(partsno) && !"".equals(inoutflag)) {
							entity.setPartsno(partsno);
							entity.setInclusionpart(inclusionpart);
							entity.setInoutflag(inoutflag);
							entity.setCarfamilycode(carfamilycode);
							entity.setOesp(oesp);
							entity.setIsok(isok);
							entity.setAddUser(username);

							list.add(entity);
							System.out.println(i);
						}
					}

				}
				//清理临时表
				gSPM026Dao.deleteTemp2();
				//分批插入
				if(list.size()<=1000){
					// 将数据插入到临时表
					gSPM026Dao.insertsTemp2(list);
				}else{
					int times = (int)Math.ceil( list.size()/1000.0 );
					for(int i=0; i list = new ArrayList();
				List strErrorInfo = new ArrayList<>();//存放转换过程中的错误信息
				String sheet1Partsno = "";


				//获取sheet1中的内制数据
				List sheet1List = gSPM026Dao.selectSheet1();
				for (SpmPartsAssembly2Entity entity2 : sheet1List) {
					sheet1Partsno = entity2.getPartsno();
					String partsAssembly = "";
					String LV1 = "0";
					int count = 0;
					boolean flag = false;
					int newLV = 1;

					for (int i = 0; i <= lastRowNum; i++) {
						Row thisRow = sheet.getRow(i);//读取行
						if (thisRow != null) {
							//读取列
							String Model = thisRow.getCell(0) == null ? "" : thisRow.getCell(0).toString();
							String GroupNo = thisRow.getCell(1) == null ? "" : thisRow.getCell(1).toString();
							String CompVari = thisRow.getCell(2) == null ? "" : thisRow.getCell(2).toString();
							String LV = thisRow.getCell(3) == null ? "" : thisRow.getCell(3).toString();
							String GC = thisRow.getCell(4) == null ? "" : thisRow.getCell(4).toString();
							String DivID = thisRow.getCell(5) == null ? "" : thisRow.getCell(5).toString();
							String PartNumber = thisRow.getCell(6) == null ? "" : thisRow.getCell(6).toString();
							String PartName = thisRow.getCell(7) == null ? "" : thisRow.getCell(7).toString();
							String ColorName = thisRow.getCell(8) == null ? "" : thisRow.getCell(8).toString();
							String Qty = thisRow.getCell(9) == null ? "" : thisRow.getCell(9).toString();
							String Sel = thisRow.getCell(10) == null ? "" : thisRow.getCell(10).toString();
							String ECINo = thisRow.getCell(11) == null ? "" : thisRow.getCell(11).toString();
							String RegDate = thisRow.getCell(12) == null ? "" : thisRow.getCell(12).toString();
							String Mfr = thisRow.getCell(13) == null ? "" : thisRow.getCell(13).toString();
							String SD = thisRow.getCell(14) == null ? "" : thisRow.getCell(14).toString();
							String SDPN = thisRow.getCell(15) == null ? "" : thisRow.getCell(15).toString();
							String AreaModelLimitation = thisRow.getCell(16) == null ? "" : thisRow.getCell(16).toString();
							String LimitationCondition = thisRow.getCell(17) == null ? "" : thisRow.getCell(17).toString();
							String SD1 = thisRow.getCell(18) == null ? "" : thisRow.getCell(18).toString();
							String ECINo1 = thisRow.getCell(19) == null ? "" : thisRow.getCell(19).toString();
							String EffectiveFrom = thisRow.getCell(20) == null ? "" : thisRow.getCell(20).toString();
							String EffectivePeriod = thisRow.getCell(21) == null ? "" : thisRow.getCell(21).toString();
							String SPNotificationNo = thisRow.getCell(22) == null ? "" : thisRow.getCell(22).toString();
							String EffectiveFrom1 = thisRow.getCell(23) == null ? "" : thisRow.getCell(23).toString();
							String EffectivePeriod1 = thisRow.getCell(24) == null ? "" : thisRow.getCell(24).toString();
							String CommentCode = thisRow.getCell(25) == null ? "" : thisRow.getCell(25).toString();
							String ECINo2 = thisRow.getCell(26) == null ? "" : thisRow.getCell(26).toString();
							String comment1 = thisRow.getCell(27) == null ? "" : thisRow.getCell(27).toString();
							String Routing = thisRow.getCell(28) == null ? "" : thisRow.getCell(28).toString();
							String RoutCheck = thisRow.getCell(29) == null ? "" : thisRow.getCell(29).toString();
							String RoutComment = thisRow.getCell(30) == null ? "" : thisRow.getCell(30).toString();
							String RCINo = thisRow.getCell(31) == null ? "" : thisRow.getCell(31).toString();
							String EffectiveFrom2 = thisRow.getCell(32) == null ? "" : thisRow.getCell(32).toString();
							String EffectivePeriod2 = thisRow.getCell(33) == null ? "" : thisRow.getCell(33).toString();
							String Drawing = thisRow.getCell(34) == null ? "" : thisRow.getCell(34).toString();


//							String PartNumber1 = PartNumber.replace("-","").replace("*","");
							//12位品番
							String twelvePartsno = StringUtils.rightPad(PartNumber.replace("-", "").replace("*", ""), 12, "0");

							//去表头和空行
							if (!"Model".equals(Model) && !"".equals(PartNumber) && !"".equals(LV)) {
								if (twelvePartsno.equals(sheet1Partsno)) {
									LV1 = LV;
									partsAssembly = PartNumber;
									flag=true;
								}

								SpmPartsAssembly1Entity entity = new SpmPartsAssembly1Entity();

								if (Double.parseDouble(LV) == Double.parseDouble(LV1)) {
									count++;
								}
								if (count == 1) {
									entity.setPartsAssembly(partsAssembly);
									entity.setModel(Model);
									entity.setGroupNo(GroupNo);
									entity.setCompVari(CompVari);
									entity.setLV(String.valueOf(newLV));
									entity.setGC(GC);
									entity.setDivID(DivID);
									entity.setPartNumber(PartNumber);
									entity.setPartName(PartName);
									entity.setColorName(ColorName);
									entity.setQty(Qty);
									entity.setSel(Sel);
									entity.setECINo(ECINo);
									entity.setRegDate(RegDate);
									entity.setMfr(Mfr);
									entity.setSD(SD);
									entity.setSDPN(SDPN);
									entity.setAreaModelLimitation(AreaModelLimitation);
									entity.setLimitationCondition(LimitationCondition);
									entity.setSD1(SD1);
									entity.setECINo1(ECINo1);
									entity.setEffectiveFrom(EffectiveFrom);
									entity.setEffectivePeriod(EffectivePeriod);
									entity.setSPNotificationNo(SPNotificationNo);
									entity.setEffectiveFrom1(EffectiveFrom1);
									entity.setEffectivePeriod1(EffectivePeriod1);
									entity.setCommentCode(CommentCode);
									entity.setECINo2(ECINo2);
									entity.setComment1(comment1);
									entity.setRouting(Routing);
									entity.setRoutCheck(RoutCheck);
									entity.setRoutComment(RoutComment);
									entity.setRCINo(RCINo);
									entity.setEffectiveFrom2(EffectiveFrom2);
									entity.setEffectivePeriod2(EffectivePeriod2);
									entity.setDrawing(Drawing);
									entity.setAddUser(username);

									list.add(entity);
									System.out.println(i);
									newLV++;
								}

								if (count == 2) {
									break;
								}
							}
						}
					}
					//sheet1和sheet2没匹配上 上传失败,提示品番
					if(!flag){
						msgInfo.setSuccess(false);
						msgInfo.setMessageContent("上传失败!工作表1中的品番:"+sheet1Partsno+"未匹配成功!");
						throw new Exception("上传失败!工作表1中的品番:"+sheet1Partsno+"未匹配成功!");
					}

				}

				//清理临时表
//				gSPM026Dao.deleteTemp1();
				gSPM026Dao.deleteTemp3();

				//分批插入
				if(list.size()<=1000){
					// 将数据插入到临时表
//					gSPM026Dao.insertsTemp1(list);
					gSPM026Dao.insertsTemp3(list);
				}else{
					int times = (int)Math.ceil( list.size()/1000.0 );
					for(int i=0; i

你可能感兴趣的:(java,开发语言)