<action name="exportExcel" class="exportExcel">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param><!-- 下载文件类型 -->
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param><!-- 下载文件名 -->
<param name="bufferSize">1024</param>
</result>
</action>
action:
public class ExportExcel extends BaseAction {
....
private InputStream inputStream;
private String filename;
getter&setter.....
public String exportExecl() {
response.reset();
response.setContentType("application/vn.ms-excel");
response.setCharacterEncoding("utf-8");
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.reset();
try {
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("第一页", 0);
int num = title.split(",").length;
for (int i = 0; i < num; i++) {
sheet.addCell(new Label(i, 0, title.split(",")[i]));
}
int xNumber = 1; // 定义循环的行的编号变量随for循环自动循环
List l = null;
l = tjService.query(sql);
if (l != null && l.size() > 0) {
System.out.println("size" + l.size());
String v[] = val.split(",");
for (int i = 0; i < l.size(); i++) {
clsObj = l.get(i);
for (int j = 0; j < v.length; j++) {
String methodname = "get" + v[j].substring(0, 1).toUpperCase() + v[j].substring(1);
Method method = clsObj.getClass().getMethod(methodname, new Class[] {});
Object o = method.invoke(clsObj, new Class[] {});
if (o == null) {
sheet.addCell(new Label(j, xNumber, ""));
} else {
sheet.addCell(new Label(j, xNumber, o.toString()));
}
}
++xNumber;
}
}
book.write();
book.close();
this.filename = new Date().toString() + ".xls";
this.inputStream = new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
System.out.println("excel表格创建或写入错误,错误信息如下");
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
}
action没有报错,就是页面不提示下载,显示乱码,头都大了,另一个页面同样配置都可以正常导出
情况如下图:
21 个解决方案
#1
action的method少了指定下<action name="exportExcel" class="exportExcel"
method="exportExcel">
#2
先要排除Excel有没有生成,或者你配置里面出现了重复转向,
页面弹出下载代码贴出来看看
页面弹出下载代码贴出来看看
#3
人品!!!
#4
我在请求的时候用的是exportExcel!exportExecl.action
#5
excel直接写到硬盘没问题,向前台传的时候不行,struts2配置下载在前台不用写弹出代码,配置了<param name="contentDisposition">attachment;filename="${filename}"</param><!-- 下载文件名 --> 就好了
#6
#7
getInputStream()方法有了吗
#8
都有了
#9
this.filename = new Date().toString() + ".xls";
难道文件名的问题?猜测
#10
也不是,这个也试过了
#11
不要沉了啊。。。。。。。
#12
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(paramfile.getName().getBytes("gb2312"),
"iso8859-1"));
os = response.getOutputStream();
book = Workbook.createWorkbook(os);
return null;
最后配置文件
<action name="XXX"
class="XXXXX" method="XXX">
</action>
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(paramfile.getName().getBytes("gb2312"),
"iso8859-1"));
os = response.getOutputStream();
book = Workbook.createWorkbook(os);
return null;
最后配置文件
<action name="XXX"
class="XXXXX" method="XXX">
</action>
#13
<param name="contentType">application/application/vnd.ms-excel</param>
这个改成excel的mimeType再把filename用个简单点的a.xls先试下能下载不
这个改成excel的mimeType再把filename用个简单点的a.xls先试下能下载不
#14
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "ISO8859-1") +
new String("导出".getBytes("gbk"), "ISO8859-1")
+ ".xls");
OutputStream out = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(out);
……………………
workbook.write();
out.flush();
上面是Struts1的写法
#15
试了下,还是不行
#16
楼主页面上怎么写的?
#17
就一个按钮,请求这个action所有查询sql都是在action拼接出来的
#18
book.write();
book.close();
this.filename = new Date().toString() + ".xls"; this.inputStream = new ByteArrayInputStream(os.toByteArray());
这段代码看上去不太像是将excel发送到前台的,os和book是怎么关联的,os又是怎么发送到前台的
可以先把业务逻辑放在servlet里测一下
book.close();
this.filename = new Date().toString() + ".xls"; this.inputStream = new ByteArrayInputStream(os.toByteArray());
这段代码看上去不太像是将excel发送到前台的,os和book是怎么关联的,os又是怎么发送到前台的
可以先把业务逻辑放在servlet里测一下
#19
请问楼主解决了没有
#20
我和楼主遇到一样的问题了,我解决办法是由原来的post异步访问后台的导出方法修改为get方式就没问题了
#21
public String getFileName() throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes(), "ISO-8859-1");
return fileName;
}
fileName = new String(fileName.getBytes(), "ISO-8859-1");
return fileName;
}
#1
action的method少了指定下<action name="exportExcel" class="exportExcel"
method="exportExcel">
#2
先要排除Excel有没有生成,或者你配置里面出现了重复转向,
页面弹出下载代码贴出来看看
页面弹出下载代码贴出来看看
#3
人品!!!
#4
我在请求的时候用的是exportExcel!exportExecl.action
#5
excel直接写到硬盘没问题,向前台传的时候不行,struts2配置下载在前台不用写弹出代码,配置了<param name="contentDisposition">attachment;filename="${filename}"</param><!-- 下载文件名 --> 就好了
#6
#7
getInputStream()方法有了吗
#8
都有了
#9
this.filename = new Date().toString() + ".xls";
难道文件名的问题?猜测
#10
也不是,这个也试过了
#11
不要沉了啊。。。。。。。
#12
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(paramfile.getName().getBytes("gb2312"),
"iso8859-1"));
os = response.getOutputStream();
book = Workbook.createWorkbook(os);
return null;
最后配置文件
<action name="XXX"
class="XXXXX" method="XXX">
</action>
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(paramfile.getName().getBytes("gb2312"),
"iso8859-1"));
os = response.getOutputStream();
book = Workbook.createWorkbook(os);
return null;
最后配置文件
<action name="XXX"
class="XXXXX" method="XXX">
</action>
#13
<param name="contentType">application/application/vnd.ms-excel</param>
这个改成excel的mimeType再把filename用个简单点的a.xls先试下能下载不
这个改成excel的mimeType再把filename用个简单点的a.xls先试下能下载不
#14
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "ISO8859-1") +
new String("导出".getBytes("gbk"), "ISO8859-1")
+ ".xls");
OutputStream out = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(out);
……………………
workbook.write();
out.flush();
上面是Struts1的写法
#15
试了下,还是不行
#16
楼主页面上怎么写的?
#17
就一个按钮,请求这个action所有查询sql都是在action拼接出来的
#18
book.write();
book.close();
this.filename = new Date().toString() + ".xls"; this.inputStream = new ByteArrayInputStream(os.toByteArray());
这段代码看上去不太像是将excel发送到前台的,os和book是怎么关联的,os又是怎么发送到前台的
可以先把业务逻辑放在servlet里测一下
book.close();
this.filename = new Date().toString() + ".xls"; this.inputStream = new ByteArrayInputStream(os.toByteArray());
这段代码看上去不太像是将excel发送到前台的,os和book是怎么关联的,os又是怎么发送到前台的
可以先把业务逻辑放在servlet里测一下
#19
请问楼主解决了没有
#20
我和楼主遇到一样的问题了,我解决办法是由原来的post异步访问后台的导出方法修改为get方式就没问题了
#21
public String getFileName() throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes(), "ISO-8859-1");
return fileName;
}
fileName = new String(fileName.getBytes(), "ISO-8859-1");
return fileName;
}