spring mvc poi excel 导入excel

jsp页面:

<form name="excelImportForm" action="${ctx}/cheeventlog/upload" method="post" enctype="multipart/form-data" id="excelImportForm">

		<div class="modal-body">
			<div class="row gap">
			
				<div class="col-sm-3">	
					<input id="excel_file" type="file" name="filename"/>
				</div>
				
				<div class="col-sm-3">

					<input id="excel_button" type="submit" value="导入Excel" />
				
				</div>
			</div>

		</div>
		</form>
private String getValue(HSSFCell hssfCell) {

		if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值

			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值

			Double temp = hssfCell.getNumericCellValue();
			return String.valueOf(temp.intValue());
		} else {
			// 返回字符串类型的值
			return hssfCell.getStringCellValue();
		}
	}
@RequestMapping(value = "upload", method = RequestMethod.POST)
	public String upload(@RequestParam("filename") MultipartFile file,
			HttpServletRequest request) throws IOException {


		String name = file.getOriginalFilename(); // 获取上传文件名
		long size = file.getSize(); // 获取文件的大小
		if ((name == null || name.equals("")) && size == 0) {
			return null;
		}
		InputStream is = file.getInputStream();
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); // 一个excel对象

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		CheEventLog cheLog = null;

		try {
			// 循环工作表Sheet
			for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
				HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
				if (hssfSheet == null) {
					continue;
				}

				// 循环行Row
				for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
					HSSFRow hssfRow = hssfSheet.getRow(rowNum);
					if (hssfRow != null) {

						HSSFCell eventSource = hssfRow.getCell(0);
						HSSFCell companyName = hssfRow.getCell(1);
						HSSFCell eventName = hssfRow.getCell(2);
						HSSFCell eventReason = hssfRow.getCell(3);
						HSSFCell eventEnterprise = hssfRow.getCell(4);
						HSSFCell emergencySituation = hssfRow.getCell(5);
						HSSFCell behindSituation = hssfRow.getCell(6);
						HSSFCell disabled = hssfRow.getCell(7);
						HSSFCell version = hssfRow.getCell(8);
						HSSFCell createUserid = hssfRow.getCell(9);
						HSSFCell createTime = hssfRow.getCell(10);
						HSSFCell updateUserid = hssfRow.getCell(11);
						HSSFCell updateTime = hssfRow.getCell(12);

						// new
						cheLog = new CheEventLog();
						
						cheLog.setEventSource(Integer .valueOf(getValue(eventSource)));
						cheLog.setCompanyName(getValue(companyName));
						cheLog.setEventName(getValue(eventName));
						cheLog.setEventReason(getValue(eventReason));
						cheLog.setEventEnterprise(getValue(eventEnterprise));
						cheLog.setEmergencySituation(getValue(emergencySituation));
						cheLog.setBehindSituation(getValue(behindSituation));
						cheLog.setDisabled(Integer.valueOf(getValue(disabled)));
						cheLog.setVersion(Integer.valueOf(getValue(version)));
						cheLog.setCreateUserid(Integer .valueOf(getValue(createUserid)));
						cheLog.setCreateTime(sdf.parse(getValue(createTime)));
						cheLog.setUpdateUserid(Integer .valueOf(getValue(updateUserid)));
						cheLog.setUpdateTime(sdf.parse(getValue(updateTime)));
						
						cheEventLogService.save(cheLog);
						
						System.out.println(createTime + "    " + updateTime);

					}
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "redirect:/cheeventlog/list";
	}

 

 

猜你喜欢

转载自xiao523899117.iteye.com/blog/2201093