2012-06-08 61 views
0

這是我的JSP頁面...上傳EXCEL文件...我需要爲不同的動作uplode 2個不同的excel文件...我希望標籤我是自我解釋...如何使用struts2讀取excel文件應用程序將其轉換爲.tmp

<body> 
<s:actionerror /> 
<s:fielderror /> 
<s:form action="Admin" method="post" enctype="multipart/form-data" theme="simple"> 
<big>Import Batch and Tan Details</big> 
<br> 
<s:file name="uploadToBatch" label="Select a Excel File for Batch import" /> 
<s:submit action="ImportBatchAndTanDetailsAdmin" value="Import" /> 
<br> 
<big>Allocate Batch</big> 
<br> 
<s:file name="uploadToAllocate" label="Select a Excel File for Allocation" /> 
<s:submit action="AllocateAdmin" value="Allocate" onclick=""/> 
</s:form> 
</body> 

下一個是這2個動作我strut.xml配置...

<action name="*Admin" method="{1}" class="controller.AdminAction"> 
      <interceptor-ref name="fileUpload"> 
       <param name="maximumSize">2572864</param> 
       <param name="allowedTypes">application/ms-excel,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param> 
      </interceptor-ref> 
      <interceptor-ref name="defaultStack"></interceptor-ref> 
      <result name="error">adminPage.jsp</result> 
      <result name="input">adminPage.jsp</result> 
      <result name="success">adminPage.jsp</result> 
     </action> 

下一個是動作類...

package controller; 

import java.io.File; 
import jxl.Sheet; 
import jxl.Workbook; 
import com.opensymphony.xwork2.ActionSupport; 

@SuppressWarnings("serial") 
public class AdminAction extends ActionSupport { 

    private File uploadToBatch;//The actual file 
    private File uploadToAllocate; 
    private String uploadContentType; //The content type of the file 
    private String uploadFileName; //The uploaded file name 
    AuthorIndexingDAO DAO = new AuthorIndexingDAO(); 

    //getters and setters... 
    public File getUploadToBatch() { 
     return uploadToBatch; 
    } 
    public void setUploadToBatch(File uploadToBatch) { 
     this.uploadToBatch = uploadToBatch; 
    } 
    public File getUploadToAllocate() { 
     return uploadToAllocate; 
    } 
    public void setUploadToAllocate(File uploadToAllocate) { 
     this.uploadToAllocate = uploadToAllocate; 
    } 
    public String getUploadContentType() { 
     return uploadContentType; 
    } 
    public void setUploadContentType(String uploadContentType) { 
     this.uploadContentType = uploadContentType; 
    } 
    public String getUploadFileName() { 
     return uploadFileName; 
    } 
    public void setUploadFileName(String uploadFileName) { 
     this.uploadFileName = uploadFileName; 
    } 



    //ececute method... 


    public String execute(){ 
     return SUCCESS; 
    } 


    //other methods.... 
    public String ImportBatchAndTanDetails(){ 

     try { 

       Workbook w; 

        w = Workbook.getWorkbook(uploadToBatch); 
        Sheet sheet = w.getSheet(0); 
        System.out.println(sheet.getName()); 


        } catch (Exception e) { 
          e.printStackTrace(); 
          System.out.println(e.getMessage()); 
          addActionError(e.getMessage()); 

        return ERROR; 

        } 

     return SUCCESS; 
    } 


public String Allocate(){ 

     try { 

      System.out.println(uploadFileName); 
      System.out.println(uploadToAllocate.getName()); 

       Workbook w; 

        w = Workbook.getWorkbook(uploadToAllocate); 
        Sheet sheet = w.getSheet(0); 
        System.out.println(sheet.getName()); 


        } catch (Exception e) { 
         e.printStackTrace(); 
        return ERROR; 

        } 

     return SUCCESS; 
    } 

} 

而我輸入我得到這個控制檯...

null 
upload__6e26fdaa_137cba00800__8000_00000001.tmp 

和近w = Workbook.getWorkbook(uploadToAllocate);拋出的異常是......

jxl.read.biff.BiffException: Unable to recognize OLE stream 
    at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116) 
    at jxl.read.biff.File.<init>(File.java:127) 
    at jxl.Workbook.getWorkbook(Workbook.java:221) 
    at jxl.Workbook.getWorkbook(Workbook.java:198) 
    at controller.AdminAction.Allocate(AdminAction.java:154) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) 
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) 
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:314) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:314) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485) 
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
+0

我找到了原因,但沒有解決方案。原因是excel不是以正確的方式創建的。它是通過創建一個擴展名爲「.xls」的文件創建的。但看到'MS Excel'可以打開它,爲什麼不用java代碼可以做到這一點? –

回答

0

我已經找到了原因。我導入的excel不是以適當的方式創建的,而是我複製了內容並將其粘貼到新的excel中並保存。後來當我導入這個新的Excel時,它工作正常。

0

您已經使用JXL API,JXL僅支持.xls擴展名及以上的,它不支持新的exensions如的.xlsx 你應該用一個不兼容的擴展最有可能的.xlsx這就是爲什麼
「jxl.read。 biff.BiffException:無法識別OLE流'異常被拋出。 只是嘗試使用擴展名爲.xls的文件,而不是使用jxl api使用poi。

相關問題