日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

POI实现数据导入功能

發布時間:2024/6/21 综合教程 37 生活家
生活随笔 收集整理的這篇文章主要介紹了 POI实现数据导入功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.導入過程(基本就是導出的逆向過程)

1.存在一個包含數據的Excel文件

2.將文件作為參數傳到服務器

3.服務器解析文件,并將數據封裝成實體對象

4.將對象持久化更新到數據庫

5.刷新頁面導入成功

二.具體代碼實現

1.前端

   <form id="uploadForm" action="user/upload.do" method="post" enctype="multipart/form-data">
        	<table>
        		<tr>
        			<td>下載模版:</td>
        			<td><a href="javascript:void(0)" class="easyui-linkbutton"  onclick="downloadTemplate()">導入模版</a></td>
        		</tr>
        		<tr>
        			<td>上傳文件:</td>
        			<td><input type="file" name="userUploadFile"></td>
        		</tr>
        	</table>
        </form>
	</div>

   2.

 @RequestMapping("/upload")
		public void upload(@RequestParam(value="userUploadFile",required=true)MultipartFile userUploadFile,HttpServletResponse response)throws Exception{
		   HSSFWorkbook wb=new HSSFWorkbook(userUploadFile.getInputStream());
			HSSFSheet hssfSheet=wb.getSheetAt(0);  // 獲取第一個Sheet頁
			if(hssfSheet!=null){
				for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
					HSSFRow hssfRow=hssfSheet.getRow(rowNum);
					if(hssfRow==null){
						continue;
					}
					User user=new User();
					user.setName(ExcelUtil.formatCell(hssfRow.getCell(0)));
					user.setPhone(ExcelUtil.formatCell(hssfRow.getCell(1)));
					user.setEmail(ExcelUtil.formatCell(hssfRow.getCell(2)));
					user.setQq(ExcelUtil.formatCell(hssfRow.getCell(3)));
					Connection con=null;
						con=dbUtil.getCon();
						userDao.userAdd(con, user);
						dbUtil.closeCon(con);
				}
			}
			wb.close();
			JSONObject result=new JSONObject();
			result.put("success", "true");
			ResponseUtil.write(response, result);
		}

   3.

public int userAdd(Connection con,User user)throws Exception{
		String sql="insert into t_user values(null,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getName());
		pstmt.setString(2, user.getPhone());
		pstmt.setString(3, user.getEmail());
		pstmt.setString(4, user.getQq());
		return pstmt.executeUpdate();
	}

  

總結

以上是生活随笔為你收集整理的POI实现数据导入功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。