将Excel文件内容写入到数据库

package    com.test.spring;

  import    java.io.File;

  import    javax.servlet.http.HttpServletRequest;
  import    javax.servlet.http.HttpServletResponse;

  import    jxl.Sheet;
  import    jxl.Workbook;

  import    org.springframework.validation.BindException;
  import    org.springframework.web.multipart.MultipartFile;
  import    org.springframework.web.multipart.MultipartHttpServletRequest;
  import    org.springframework.web.servlet.ModelAndView;
  import    org.springframework.web.servlet.mvc.SimpleFormController;

  import    com.ctgusec.model.Student_info;
  import    com.ctgusec.service.IStudent_infoManage;


 
public       class    EStudentInsertExcelController    extends    SimpleFormController    {

     private  IStudent_infoManage studentManage;

    @Override
     protected  ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
             throws  Exception  {
        Student_info student_info  =  (Student_info) command;
         try   {            
            MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;
            MultipartFile file  =  multipartRequest.getFile( " Excelfile " );  //  获得文件: 
 
            File toFile  =   new  File( " c:\\学生信息临时文件.xls " ); //  产生文件名和空文件 
 
            file.transferTo(toFile); //  文件上传 
 
            Workbook book  =  Workbook.getWorkbook(toFile); // 得到工作薄             
 
            Sheet sheet  =  book.getSheet( 0 ); //  获得第一个工作表对象 
 
             int  row  =  sheet.getRows(); //  /得到该sheet的行数 
 
             int  column  =  sheet.getColumns();  //  得到该sheet的列数     
 
            System.out.println( " 数据行数= " + row);
            System.out.println( " 数据列数= " + column);
             for ( int  i = 1 ;i < row;i ++ )
             {
                 for ( int  j = 0 ;j < column;j ++ )
                 {
                    System.out.println( " j= " + j);
                    sheet.getCell(j, i).getContents(); //  得到第j列第i行的单元格的类容         
 
                    student_info.setStudentID(sheet.getCell(j, i).getContents());    
                    student_info.setName(sheet.getCell( ++ j,i).getContents());    
                    student_info.setSex(sheet.getCell( ++ j,i).getContents());    
                    student_info.setUnit(sheet.getCell( ++ j,i).getContents());    
                    student_info.setClass_(sheet.getCell( ++ j,i).getContents());    
                    student_info.setSpecialty(sheet.getCell( ++ j,i).getContents());    
                    student_info.setRemark(sheet.getCell( ++ j,i).getContents());                        
                } 
    
                 if  ( this .studentManage.getStudentByStudentID(
                        student_info.getStudentID()).size()  !=   0 )
                     return   new  ModelAndView( " education/e-studentInfoAddError " );
                 this .studentManage.insertStudent_info(student_info);    
            } 
                
            book.close();
             return   new  ModelAndView( " education/e-studentInfoAddExcelSuccess " , " row " , new  Integer(row - 1 ));
        } 
  catch  (Exception e)  {                    
            e.printStackTrace();
        } 

         return   new  ModelAndView( " education/e-studentInfoAddExcelError " );
    } 

 
      public   void  setStudentManage(IStudent_infoManage studentManage)  {
         this .studentManage  =  studentManage;
    } 
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值