从JSP页面上传文件, java中获取文件,有多个sheet,循环得到每个cell的数据  求大神给个例子,是struts的StrutspoiJava

解决方案 »

  1.   

    简单写了一个,楼主不明白的问我。
    jsp :
    <s:form action="doUpload" method="POST" enctype="multipart/form-data">
    <s:file name="upload" label="File"/>
    <s:submit value = "Upload" />
    </s:form>就写了一些重点的东西
    struts-fileupload.xml<struts>
    <package name="fileupload" extends="struts-default" namespace="/fileupload">
            
            <action name="doUpload" class="actions.fileupload.FileUploadAction" method="execute">
            
    <result>upload-success.jsp</result>
    </action>
        </package>
    </struts>FileUploadAction.javapublic Workbook createWorkBook(InputStream is) throws IOException{    
           if(fileName.toLowerCase().endsWith("xls")){    
                return new HSSFWorkbook(is);    
           }else if(fileName.toLowerCase().endsWith("xlsx")){  
                return new XSSFWorkbook(is);  
           }else{  
                return null;  
           }  
        } 

    public String execute() throws Exception{

    Workbook book = createWorkBook(new FileInputStream(upload));  
            if(null !=book){
             if (book.getNumberOfSheets()> 0){
                            //写个循环,获取所有的,这边我就获取第一个sheet,为了简单
             Sheet aSheet = book.getSheetAt(0);
             int lastRows = aSheet.getLastRowNum();
            
             for(int j=1;j<lastRows;j++){
             Row aRow = aSheet.getRow(j);
             if(null!=aRow){
             if(null !=aRow.getCell(1)){
             //这边你就可以获取有用的值了
    //比如用aRow.getCell(1).getStringCellValue(),可以把数据存到数据库中。
    //要注意,对于不同的数值类型,方法不同,不然会报错,最好看看api
                
             }
            
             }
            
            
             }
            
             }
            }
             return SUCCESS;
    }
      

  2.   

    文件的话就是前台jsp上传file型的,文件命名name="excel"
    后台:
    File excel;//提供get,set方法
    exceute方法里面
    http://bbs.csdn.net/topics/390360927
      

  3.   

    如果我想要在Action里面获取 path,name,File  应该怎么弄呢
      

  4.   

    你jsp中定义好 path ,name ,后台 get set  就可以获取到了。
      

  5.   

    1楼的例子很好,这个可以理解为先上传附件,然后解析execl
      

  6.   

    如果我想要在Action里面获取 path,name,File  应该怎么弄呢
    恩,因为我们jsp中用的<s:file name="upload" .../>
    所以我们可以直接用get,set获取文件信息,比如在action中
    获取文件,可以这样获取public File getUpload() {
    return upload;
    } public void setUpload(File upload) {
    this.upload = upload;
    }获取文件名称,可以这样 public String getUploadFileName() {
    return fileName;
    } public void setUploadFileName(String fileName) {
    this.fileName = fileName;
    }获取文件类型 public String getUploadContentType() {
    return contentType;
    } public void setUploadContentType(String contentType) {
    this.contentType = contentType;
    }楼主明白了吧?呵呵