POI实现数据导入功能
生活随笔
收集整理的這篇文章主要介紹了
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实现数据导入功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件夹设置密码方法教程电脑文档如何设置密
- 下一篇: 电脑怎么压缩视频电脑上如何压缩视频