我需要导出excel表,所以通过Ajax调用从对话框窗口调用action类方法。当我点击图标调用方法并处理后端数据并最终执行以下代码并返回结果集以下载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; }
在构建方法中有创建工作簿
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.
答案 0 :(得分:2)
以二进制流的形式返回结果
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;