ssm之九 批量导入excel到数据库
生活随笔
收集整理的這篇文章主要介紹了
ssm之九 批量导入excel到数据库
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JAVA就業(yè)套餐課:https://edu.csdn.net/combo/detail/1230
本案例承接以前的SSM整合系列,針對(duì)Oracle中的Dept表做操作,如果單擊瀏覽,則進(jìn)行批量導(dǎo)入;單擊2則是單個(gè)導(dǎo)入。
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>部門(mén)添加</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript" src="easyui/jquery-1.8.3.min.js"></script><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript"><%--function onload(){var temp = '${errormsg}';if(temp!=''){alert(temp);}}--%>$(function(){$("#subbtn").click(function(){var deptNo = $(".deptNo").val();if($.trim(deptNo)==''){// alert("部門(mén)編號(hào)不能為空!");$("#spana").html("部門(mén)編號(hào)不能為空!");return;}else{if(deptNo.length<2){$("#spana").html("部門(mén)編號(hào)不能小于2位數(shù)!");return;}}var dName = $("#dName").val();if(dName==''){// alert("部門(mén)編號(hào)不能為空!");$("#spana").html("部門(mén)名稱(chēng)不能為空!");return;}var loc = $("#loc").val();if(loc==''){// alert("部門(mén)編號(hào)不能為空!");$("#spana").html("部門(mén)位置不能為空!");return;}$("#form1").submit();});});</script></head><body onload="onload()"><div align="center">部門(mén)添加 <c:if test="${errormsg}">1111</c:if> <span id="spana" style="color:#ff0000">${errormsg}</span> </div><form action="dept/deptImp" method="post" enctype="multipart/form-data"><div align="center"> <a href="jsp/deptinfo.xls">模板下載</a> <input type="file" name="mFile"><input type="submit" value="提交"> </div> </form><form action="dept/add" method="post" id="form1"><table border="1" align="center" width="80%"><tr align="center"><td>部門(mén)編號(hào)</td><td> <input type="text" name="deptNo" class="deptNo"> </td> </tr> <tr align="center"><td>部門(mén)名稱(chēng)</td><td> <input type="text" name="dName" id="dName"> </td> </tr> <tr align="center"><td>部門(mén)位置</td><td> <input type="text" name="loc" id="loc"> </td> </tr> <tr align="center"><td colspan="2"> <input type="button" id="subbtn" value="提交"> </td></tr></table></form></body> </html>批量導(dǎo)入部門(mén)的控制器代碼:
?
?
package com.aaa.ssm.controller;import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map;import javax.servlet.http.HttpServletRequest;import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile;import com.aaa.ssm.entity.Dept; import com.aaa.ssm.service.IDeptService; import com.aaa.ssm.util.PageData; import com.aaa.ssm.util.PageUtil;/***@classname:DeptController.java*@discription:部門(mén) 控制器,負(fù)責(zé)與前后臺(tái)及模型交互**@author:zhz*@createtime:2017-4-13上午09:09:16*@version:1.0*/ @Controller @RequestMapping("/dept") public class DeptController {@Autowiredprivate IDeptService service;/*** 部門(mén)列表方法* @return*/@RequestMapping("/list")public String list(Model model,Integer pageNo,HttpServletRequest request){/*System.out.println("URI地址:"+request.getRequestURI());Enumeration pNames=request.getParameterNames(); while(pNames.hasMoreElements()){ String name=(String)pNames.nextElement();System.out.println("name:"+name+"="+request.getParameter(name));}*/int pageSize=2;if(pageNo==null){pageNo=1;}Map map =new HashMap();map.put("start", (pageNo-1)*pageSize);map.put("end", pageNo*pageSize+1);PageData pageData = service.getList(map);int count = pageData.getCount();//總數(shù)量String pageString = new PageUtil(pageSize, pageNo, count, request).getPageString();model.addAttribute("deptList", pageData.getList());model.addAttribute("pageString", pageString);return "dept/list";}/*** 批量導(dǎo)入部門(mén)信息* @param mFile* @return* @throws IOException* @throws BiffException */@RequestMapping("/deptImp")public String deptImport(@RequestParam MultipartFile mFile) throws IOException, BiffException{//獲取文件流InputStream inputStream = mFile.getInputStream();//利用JAR提供的Workbook類(lèi)讀取文件Workbook workbook = Workbook.getWorkbook(inputStream);//獲取工作薄Sheet sheet = workbook.getSheet(0);//獲取總行數(shù)int rows = sheet.getRows();//獲取總列數(shù)int cells = sheet.getColumns();//循環(huán)讀取數(shù)據(jù)Dept dept = null;for(int i=1;i<rows;i++){dept = new Dept();Cell[] cell=sheet.getRow(i);dept.setDeptNo(Integer.valueOf(cell[0].getContents()));dept.setdName(cell[1].getContents());dept.setLoc(cell[2].getContents());service.add(dept);/*for(int j=0;j<cells;j++){System.out.println(cell[j].getContents());}*/}return "redirect:list";}/*** 返回DeptJson* @return*/@ResponseBody@RequestMapping("/deptJson")public Object deptJson(){Map map =new HashMap();map.put("start", 0);map.put("end", 100);PageData pageData = service.getList(map);int count = pageData.getCount();//總數(shù)量List<Dept> deptList = pageData.getList();Map mapJson =new HashMap();mapJson.put("total", count);mapJson.put("rows", deptList);return mapJson;}/*** 跳轉(zhuǎn)添加方法* @return*/@RequestMapping("/toAdd")public String toAdd(){return "dept/add";}/*** 添加方法* @param dept* @return*/@RequestMapping("/add")public String add(Dept dept,Model model){try {service.add(dept);return "redirect:list";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();model.addAttribute("errormsg", "添加部門(mén)失敗!");return "dept/add";}}/*** 跳轉(zhuǎn)部門(mén)更新* @return*/@RequestMapping("/toUpdate")public String toUpdate(Model model,int deptNo){Dept dept = service.getById(deptNo);model.addAttribute("dept", dept);return "dept/update";}/*** 部門(mén)更新* @param dept* @return*/@RequestMapping("/update")public String update(Dept dept){service.update(dept);return "redirect:list";}/*** 部門(mén)刪除* @return*/@RequestMapping("/del")public String delete(Integer deptNo){service.delete(deptNo);return "redirect:list";} } /*** 批量導(dǎo)入部門(mén)信息* @param mFile* @return* @throws IOException* @throws BiffException */@RequestMapping("/deptImp")public String deptImport(@RequestParam MultipartFile mFile) throws IOException, BiffException{//獲取文件流InputStream inputStream = mFile.getInputStream();//利用JAR提供的Workbook類(lèi)讀取文件Workbook workbook = Workbook.getWorkbook(inputStream);//獲取工作薄Sheet sheet = workbook.getSheet(0);//獲取總行數(shù)int rows = sheet.getRows();//獲取總列數(shù)int cells = sheet.getColumns();//循環(huán)讀取數(shù)據(jù)Dept dept = null;for(int i=1;i<rows;i++){dept = new Dept();Cell[] cell=sheet.getRow(i);dept.setDeptNo(Integer.valueOf(cell[0].getContents()));dept.setdName(cell[1].getContents());dept.setLoc(cell[2].getContents());service.add(dept);/*for(int j=0;j<cells;j++){System.out.println(cell[j].getContents());}*/}return "redirect:list";}/*** 返回DeptJson* @return*/@ResponseBody@RequestMapping("/deptJson")public Object deptJson(){Map map =new HashMap();map.put("start", 0);map.put("end", 100);PageData pageData = service.getList(map);int count = pageData.getCount();//總數(shù)量List<Dept> deptList = pageData.getList();Map mapJson =new HashMap();mapJson.put("total", count);mapJson.put("rows", deptList);return mapJson;}/*** 跳轉(zhuǎn)添加方法* @return*/@RequestMapping("/toAdd")public String toAdd(){return "dept/add";}/*** 添加方法* @param dept* @return*/@RequestMapping("/add")public String add(Dept dept,Model model){try {service.add(dept);return "redirect:list";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();model.addAttribute("errormsg", "添加部門(mén)失敗!");return "dept/add";}}/*** 跳轉(zhuǎn)部門(mén)更新* @return*/@RequestMapping("/toUpdate")public String toUpdate(Model model,int deptNo){Dept dept = service.getById(deptNo);model.addAttribute("dept", dept);return "dept/update";}/*** 部門(mén)更新* @param dept* @return*/@RequestMapping("/update")public String update(Dept dept){service.update(dept);return "redirect:list";}/*** 部門(mén)刪除* @return*/@RequestMapping("/del")public String delete(Integer deptNo){service.delete(deptNo);return "redirect:list";} }
服務(wù)層Service的代碼:
?
?
package com.aaa.ssm.service;import java.util.List; import java.util.Map;import com.aaa.ssm.entity.Dept; import com.aaa.ssm.util.PageData;/***@classname:IDeptService.java*@discription:部門(mén)業(yè)務(wù)類(lèi)接口**@author:zhz*@createtime:2017-4-13上午09:06:39*@version:1.0*/ public interface IDeptService {/*** 根據(jù)部門(mén)編號(hào)獲取部門(mén)對(duì)象* @return*/public Dept getById(int deptNo);/*** 部門(mén)列表* @return*/public PageData getList(Map map);/*** 部門(mén)添加* @param dept*/public void add(Dept dept);/*** 部門(mén)更新* @param dept*/public void update(Dept dept);/*** 部門(mén)刪除* @param deptNo*/public void delete(int deptNo);}服務(wù)層實(shí)現(xiàn)類(lèi)和以前的一樣:
?
?
/*** 部門(mén)添加*/public void add(Dept dept) {// TODO Auto-generated method stubdao.add(dept);// System.out.println(1/0);}
擴(kuò)展篇,該段示例代碼只能實(shí)現(xiàn),批量導(dǎo)入dept表中的數(shù)據(jù)到數(shù)據(jù)庫(kù),如何實(shí)現(xiàn)批量如何多個(gè)表呢?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的ssm之九 批量导入excel到数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 比较两个日期大小和获取当前月最大天数的存
- 下一篇: mysql navicat编码保持一致不