SpringMVC+JXL 上传Excel2003文件并导入数据库

index.jsp

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Insert title here</title> 
</head>
<body>  

 选择文件上传:   <form action="${pageContext.request.contextPath}/wenwu/importExcel.action" method="post" enctype="multipart/form-data"> 
            <input type="file" name="file" />
            <input type="submit"  value="导入Excel" />
           </form> 
                  
</body> 
</html>    

 

WenwuController.java 

/*
  * 单文件导入Excel
  */
    @RequestMapping(value = "/importExcel")
    public String importExcel(HttpServletRequest request) throws Exception{
     List<Wenwu> list=new ArrayList<Wenwu>();
     
        //将当前上下文初始化给  CommonsMutipartResolver(多部分解析器)
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();                     

            while(iter.hasNext()){
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null){
                 //得到上传文件名
                 String filename=file.getOriginalFilename();
                 String path="E:/springUpload"+filename;
                    //上传
                    file.transferTo(new File(path));
                    wenwuService.importExcelT(path);
   
                 }  
              }          
          }
         wenwuService.saveAll(list);
         System.out.println("ok");
         return "success";
    }

 

WenwuServiceImpl.java

@Override
 public void importExcelT(String path) throws IOException {  
  //定义集合批量处理数据
  List<Wenwu> list=new ArrayList<Wenwu>();
         
  int  num=0;                            
        Workbook book=null;
  try {
   book = Workbook.getWorkbook(new File(path));
   //遍历excel表里有几个工作簿
   num=book.getNumberOfSheets();
   
  } catch (BiffException e) {
   // TODO Auto-generated catch block     
   e.printStackTrace();   
   
  }                  
                                     
        //遍历工作表
        for(int i=0;i<num;i++){               
         //获得第i个工作表对象                 
   Sheet sheet = book.getSheet(i);                                          
          
   int rowcount = sheet.getRows();//行数
   //int colcount = sheet.getColumns();//列数
                             
      for(int rowindex = 2; rowindex<=rowcount;rowindex++){
       int colindex = 1;
      
       //得到第1行第1列的单元格
    Cell cell = sheet.getCell(0, 0);
    //System.out.println("==============="+cell.getContents());
       Cell cell1 = sheet.getCell(colindex-1, rowindex-1);
          Cell cell2 = sheet.getCell(colindex-0, rowindex-1);
          Cell cell3 = sheet.getCell(colindex+1, rowindex-1);
         
          //实体属性初始化
    String type=book.getSheet(i).getName();
    String name = null;
       String property = null;
    String data = null;
 
         if("名称".equals(cell.getContents())){                                              
     name = cell1.getContents();
        property = cell2.getContents();
     data = cell3.getContents();
       }else{
        Cell cell4 = sheet.getCell(colindex+2, rowindex-1);
        
        name = cell2.getContents();
        property = cell3.getContents();
     data = cell4.getContents();
       }
    
         Wenwu wenwu =new Wenwu();             
    wenwu.setType(type);
    wenwu.setName(name);
    wenwu.setProperty(property);
    wenwu.setData(data);
                      
    if(property!=null && property!=""){ 
              list.add(wenwu);              
         }                                           
         }    
     }
     wenwuDao.saveAll(list);
 }

转载于:https://www.cnblogs.com/qianxun-2017/p/7873558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值