我需要导出Excel工作表,所以从对话框窗口通过Ajax调用调用操作类方法。我有Excel图标,当我点击图标调用方法并处理后端数据时,最后执行下面的代码并返回结果以下载Excel表。
jQuery Ajax调用:
function callajax() {
jQuery.ajax({
url : '<s:url action="part" method="export"/>',
结果方法:
@Result(name="success",type=StreamResult.class,value="",params={"inputName","inputStream"})
Java代码:
public String method {
-----------------
method to call backend...
--------------------------
httpServletResponse.setContentType("application/vnd.ms-excel");
String filename = filters.getPeriod() + "_" +filters.getRegion() ;
httpServletResponse.setHeader("Content-disposition",
"attachment; filename="+filename+".xls");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
builder.build(method1, method2, outputStream);
outputStream.flush();
return SUCCESS; }
In the build method has creating workbook
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook =
Workbook.createWorkbook(outputStream, workbookSettings);
并设置表单中的所有值。请告知。
按照您的建议尝试:
我使用的是代码隐藏,所以在类名上键入结果:
@Result(name="success",type=StreamResult.class,value="",params = {"contentType","application/octet-stream","inputName","inputStream","bufferSize","1024","contentDisposition","attachment;filename="${filename}""})
public String method {
-----------------
method to call backend...
--------------------------
String filename = filters.getRegion() + "_" +
filters.getCurrency();
File file = new File(filename);
inputStream = new FileInputStream(file); //getting error "java.io.FileNotFoundException:file "
builder.buildXL(OBJ1, OBJ2, file);
return SUCCESS;
}
buildxl(obj1,obj2,File file)
{
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook =
Workbook.createWorkbook(file, workbookSettings);
-----
------
workbook.write();
workbook.close();
}
参数值:
params =
{"contentType","application/octet-stream",
"inputName","inputStream",
"bufferSize","1024",
"contentDisposition","attachment;filename="${filename}""})
日志:
2013-07-18 10:25:33,453 ERROR org.apache.struts2.dispatcher.StreamResult - Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
2013-07-18 10:25:33,453 ERROR org.apache.struts2.rest.RestActionInvocation - Exception processing the result.
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:241)
at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:198)
at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:146)
at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:147)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3288)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
2013-07-18 10:25:33,499 DEBUG org.apache.struts2.rest.RestActionInvocation - No result returned for action at null
ClasspathPackageProvider
:
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentType:application/octet-stream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[inputName:inputStream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[bufferSize:1024] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentDisposition:attachment;filename="${filename}"] to result.
以二进制流形式返回结果
private InputStream inputStream;
private String filename;
//getters here
public InputStream getInputStream() {
return inputStream;
}
public String getFilename() {
return filename;
}
@Result(name="success", type=StreamResult.class, value="inputStream", params={"contentType","application/octet-stream", "contentDisposition", "attachment;filename="${filename}"", "inputName", "inputStream"})
String filename = filters.getPeriod() + "_" +filters.getRegion() ;
File file = new File(filename);
inputStream = new FileInputStream(file);
return SUCCESS;