java 导入excel 插件 jxl

这两天用jxl简单的实现了Excel文件的导入导出,下面是我的一些总结(当然有很多是参照别人的代码)。
jsp页面代码:

Java代码 
  1. /Excel文件导入到数据库中  
  2. function importEmp(){  
  3.     //检验导入的文件是否为Excel文件  
  4.     var excelPath = document.getElementById("excelPath").value;  
  5.     if(excelPath == null || excelPath == ''){  
  6.         alert("请选择要上传的Excel文件");  
  7.         return;  
  8.     }else{  
  9.         var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase();   
  10.         if(fileExtend == '.xls'){  
  11.         }else{  
  12.             alert("文件格式需为'.xls'格式");  
  13.             return;  
  14.         }  
  15.     }  
  16.     //提交表单  
  17.     document.getElementById("empForm").action="<%=request.getContextPath()%>/EmpExcel.action.EmpExcelAction.do?method=importEmployeeInfos";    
  18.     document.getElementById("empForm").submit();  
  19. }  
  20.   
  21.    <input type="file" id="excelPath" name="excelPath"/>&nbsp;&nbsp;  
  22.    <input type="button"  value="导入Excel" onclick="importEmp()"/>  

action:

Java代码  收藏代码
  1. /** 
  2.      * Excel中的数据导入到数据库中(Excel中的字段已限定) 
  3.      * @param mapping 
  4.      * @param form 
  5.      * @param request 
  6.      * @param response 
  7.      * @return 
  8.      * @throws Exception 
  9.      */  
  10.     public ActionForward importEmployeeInfos(ActionMapping mapping, ActionForm form,   
  11.             HttpServletRequest request, HttpServletResponse response) throws Exception {  
  12.           
  13.         logger.debug(">>>importEmployeeInfos()");  
  14.         //从页面接收参数:文件的路径  
  15.         String excelPath = request.getParameter("excelPath");  
  16.         //输入流  
  17.         InputStream fis = new FileInputStream(excelPath);  
  18.         //得到解析Excel的实体集合  
  19.         List<EmployeeInfo> infos = ImportEmployee.importEmployee(fis);  
  20.           
  21.         //遍历解析Excel的实体集合  
  22.         for(EmployeeInfo info:infos) {  
  23.             //判断员工编号是否存在(存在:做修改操作;不存在:做新增操作)  
  24.             EmployeeInfo info1 = this.selectEmpByEmpNum(info.getEmployeeNumber());  
  25.             if(info1 == null) {  
  26.                 //把实体新加到数据库中  
  27.                 this.service.addEmployeeInfo(info);  
  28.             }else{  
  29.                 //把personId封装到实体  
  30.                 info.setPersonId(info1.getPersonId());  
  31.                 //更新实体  
  32.                 this.updatEmployeeInfo(info);  
  33.             }  
  34.         }  
  35.         //关闭流  
  36.         fis.close();  
  37.         logger.debug("<<<importEmployeeInfos()");        
  38.         return this.findEmployeeInfos(mapping, form, request, response);  
  39.     }  
  40.       
  41.     /** 
  42.      * 根据员工编号查找一个员工实体 
  43.      * @param employeeNumber 员工编号 
  44.      * @return 实体 
  45.      */  
  46.     private EmployeeInfo selectEmpByEmpNum(String employeeNumber) {  
  47.           
  48.         logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");  
  49.           
  50.         EmployeeInfo employeeInfo = new EmployeeInfo();  
  51.         employeeInfo.setEmployeeNumber(employeeNumber);  
  52.           
  53.         EmployeeInfo info =this.service.selectEmployeeInfoByEmpNum(employeeInfo);  
  54.           
  55.         logger.debug("<<<selectEmpByEmpNum(String employeeNumber)");  
  56.           
  57.         return info;  
  58.     }  
  59.       
  60.     /** 
  61.      * 更新一条员工信息 
  62.      * @param employeeInfo 已封装的实体 
  63.      *  
  64.      */  
  65.     private void updatEmployeeInfo(EmployeeInfo employeeInfo) {  
  66.           
  67.         logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");  
  68.           
  69.         this.service.updateEmployeeInfo(employeeInfo);  
  70.           
  71.         logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");  
  72.           
  73.     }  

 导入类:

 

Java代码 
  1. public class ImportEmployee {  
  2.     /** 
  3.      * 解析Excel文件中的数据并把每行数据封装成一个实体 
  4.      * @param fis 文件输入流 
  5.      * @return List<EmployeeInfo> Excel中数据封装实体的集合 
  6.      */  
  7.     public static List<EmployeeInfo> importEmployee(InputStream fis) {  
  8.           
  9.         List<EmployeeInfo> infos = new ArrayList<EmployeeInfo>();  
  10.         EmployeeInfo employeeInfo = null;  
  11.           
  12.         try {  
  13.             //打开文件  
  14.             Workbook book = Workbook.getWorkbook(fis);  
  15.             //得到第一个工作表对象  
  16.             Sheet sheet = book.getSheet(0);  
  17.             //得到第一个工作表中的总行数  
  18.             int rowCount = sheet.getRows();  
  19.             //日期格式化  
  20.             DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");  
  21.             //循环取出Excel中的内容  
  22.             for (int i = 1; i < rowCount; i++) {  
  23.                 employeeInfo = new EmployeeInfo();  
  24.                 Cell[] cells = sheet.getRow(i);  
  25.                 employeeInfo.setOrgId(Long.parseLong(cells[0].getContents()));  
  26.                 employeeInfo.setEmployeeNumber(cells[1].getContents().toString());  
  27.                 employeeInfo.setFullName(cells[2].getContents().toString());  
  28.                 employeeInfo.setSex(cells[3].getContents().toString());  
  29.                 employeeInfo.setDateOfBirth(new Date());  
  30.                 employeeInfo.setTownOfBirth(cells[5].getContents().toString());  
  31.                 employeeInfo.setNationalIdentifier(cells[6].getContents().toString());  
  32.                 infos.add(employeeInfo);  
  33.             }  
  34.               
  35.         } catch (BiffException e) {  
  36.             e.printStackTrace();  
  37.         } catch (IOException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.         return infos;  
  41.     }  
  42. }  

posted @ 2013-07-07 14:17  only.lonely  阅读(1166)  评论(0编辑  收藏  举报