e代剑客——温柔一刀

生活就像海洋,只有意志坚强的人,才能到达彼岸

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  76 随笔 :: 7 文章 :: 215 评论 :: 0 Trackbacks
package  com.zhupan.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;

/**
 * 
@author  zhupan
 * 
 
*/

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;
    }
    
}

posted on 2006-10-07 15:05 温柔一刀 阅读(925) 评论(1)  编辑  收藏 所属分类: java相关

评论

# re: 将Excel文件内容写入到数据库 2006-10-27 18:20 junitfans
这个更直接啊  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航:
 
联系偶 zhupanjava@gmail.com 温柔一刀