首页 > 代码库 > poi 获取excel数据 导入数据库

poi 获取excel数据 导入数据库

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {			MultipartFile file = entity.getValue();// 获取上传文件对象			ImportParams params = new ImportParams();			params.setHeadRows(1);			params.setNeedSave(false);			try {				/*System.out.println(file.getInputStream());				List<CasHistoryDateEntity> listCasHistoryDateEntitys = ExcelImportUtil.importExcel(file.getInputStream(),CasHistoryDateEntity.class,params);				listCasHistoryDateEntitys.remove(0);				for (CasHistoryDateEntity casHistoryDate : listCasHistoryDateEntitys) {*/				//casHistoryDateService.save(casHistoryDate);				// 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区)				MultipartFile file1 = file;				CommonsMultipartFile cf = (CommonsMultipartFile) file1;				DiskFileItem fi = (DiskFileItem) cf.getFileItem();				File file2 = fi.getStoreLocation();				Workbook book = null;				//book = new XSSFWorkbook(new FileInputStream(file2));				try {					// Excel 2007获取方法					book = new XSSFWorkbook(new FileInputStream(file2));				} catch (Exception ex) {					// Excel 2003获取方法					book = new HSSFWorkbook(new FileInputStream(file2));				}				// 读取表格的第一个sheet页				Sheet sheet = book.getSheetAt(0);				// 定义 row、cell				Row row;				String cell;				// 总共有多少行,从0开始				int totalRows = sheet.getLastRowNum();				// 循环输出表格中的内容,首先循环取出行,再根据行循环取出列				for (int i = 1; i <= totalRows; i++) {					CasHistoryDateEntity casHistoryDateEntity = new CasHistoryDateEntity();					Map<String, Object> map = new HashedMap();					row = sheet.getRow(i);					// 处理空行					if (row == null) {						continue;					}					// 总共有多少列,从0开始					int totalCells = row.getLastCellNum();					for (int f = row.getFirstCellNum(); f < totalCells; f++) {						// 处理空列						if (row.getCell(f) == null) {							continue;						}						// 通过 row.getCell(j).toString() 获取单元格内容						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");						cell = row.getCell(f).toString();						if (row.getRowNum() > 1) {							if (1 == f) {								casHistoryDateEntity.setXmmc(cell);							}							if (2 == f) {								casHistoryDateEntity.setQtys(cell);							}							if (3 == f) {								casHistoryDateEntity.setZzdw(cell);							}							if (4 == f) {								casHistoryDateEntity.setFzcs(cell);							}							if (5 == f) {								casHistoryDateEntity.setLxsjj(cell);							}							if (6 == f) {								casHistoryDateEntity.setLxshc(cell);							}							if (7 == f) {								casHistoryDateEntity.setLxsjn(cell);							}							if (8 == f) {								casHistoryDateEntity.setWyh(cell);							}							if (9 == f) {								casHistoryDateEntity.setXmbh(cell);							}							if (10 == f) {								casHistoryDateEntity.setJf(cell);							}							if (11 == f) {								casHistoryDateEntity.setJtsjj(cell);							}							if (12 == f) {								casHistoryDateEntity.setJtsjc(cell);							}							if (13 == f) {								casHistoryDateEntity.setJtsjn(cell);							}							if (14 == f) {								casHistoryDateEntity.setMqzt(cell);							}							if (15 == f) {								casHistoryDateEntity.setZxbg(cell);							}							if (16 == f) {								casHistoryDateEntity.setXmlxr(cell);							}							if (17 == f) {								casHistoryDateEntity.setLxfs(cell);							}							if (18 == f) {								casHistoryDateEntity.setFzr(cell);							}							if (19 == f) {								casHistoryDateEntity.setBz(cell);							}							systemService.save(casHistoryDateEntity);						}						System.out.print(cell + "\t");					}					//casHistoryDateService.save(casHistoryDate);					//casHistoryDateService.save(casHistoryDate);					System.out.println("");				}				j.setMsg("文件导入成功!");				/*}*/			} catch (Exception e) {				j.setMsg("文件导入失败!");				logger.error(ExceptionUtil.getExceptionMessage(e));			} finally {				try {					file.getInputStream().close();				} catch (IOException e) {					e.printStackTrace();				}			}		}

  

poi 获取excel数据 导入数据库