struts2 配置导出excel的问题.

limeng_java 2012-06-28 05:10:47
用jxl做了个导出excel
我的导出按钮所在的页面,被包含在一个iframe中.我的导出是直接导出的.没有事先保存到服务器上.所以打开的时候会提示"您尝试打开的文件'xxx.action'的格式与文件扩展名指定的格式不一致....."

在IE中 点击导出按钮,选择保存,这个很正常
但是选择打开,也能打开.但是第一次打开之后,打开的excel不关闭,再点 导出按钮,然后在选择打开,打开的excel就是空白的

在遨游3中 点击导出按钮,选择保存,这个很正常
但是选择打开,也能打开.但是第一次打开之后,打开的excel不关闭,再点 导出按钮,然后在选择打开,这个时候,整个web程序就好像死掉了一样,点哪里都没有反映了,就被卡死了一样

如果把那个下载的页面直接从浏览器打开,而不被包含在iframe中,就什么问题也没有
有人知道应该如何配置吗?

我的struts.xml中下载配置如下
<action name="toExcel" class="boss-action" method="toExcel">
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">filename="export.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>


...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlexChowKey 2012-07-10
  • 打赏
  • 举报
回复
导出excel不用那么麻烦配置,直接在action中写一个方法就可以实现这个功能
代码:
public String action_executeExcel(){
try{

List<JWJX_JXdajbxx> list = teachDossierService.findAll();
getDmWjlxdmAll();
getDmBgqxAll();

//设置导出头部
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");
String fileName = java.net.URLEncoder.encode("教学档案.xls", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("UTF-8"), "GBK"));
//组装Excel
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("教学档案", 0);
String titles[] = new String[] { "分类编号", "文件类型", "文件名称","归档范围","保管期限"};

String properties[] = new String[] { "flbh", "wjlx", "wjmc","gdfw", "bgqx"};

for (int i = 0; i < titles.length; i++) {
sheet.addCell(new Label(i, 0, titles[i]));
}
if(list != null && list.size()>0){
Iterator i = list.iterator();
int line = 1;
while (i.hasNext()) {
JWJX_JXdajbxx tsc = (JWJX_JXdajbxx) i.next();
for (int j = 0; j < properties.length; j++) {
Object value = PropertyUtils.getSimpleProperty(tsc,properties[j]);
value = value != null ? value : "";
if(properties[j].equals("wjlx")){
if(wjlxdms != null){
for(int a=0;a<wjlxdms.size();a++){
DM_wjlxdm dmwjlx = (DM_wjlxdm)wjlxdms.get(a);
if(tsc.getWjlx() .equals(dmwjlx.getWjlxdmid()) ){
value = dmwjlx.getMc();
}
}
}
}
if(properties[j].equals("bgqx")){
if(bgqxs != null){
for(int a=0;a<bgqxs.size();a++){
DM_bgqx dmbgqx = (DM_bgqx)bgqxs.get(a);
if(tsc.getBgqx().equals(dmbgqx.getBgqxid()) ){
value = dmbgqx.getMc();
}
}
}
}
sheet.addCell(new Label(j, line, value.toString()));
}
line++;
}
}

// 将组装好的Excel文件以流的格式输出到客户端
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


limeng_java 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Java code
jar全了没
1.所需jar包
struts2需要jar包:commons-logging.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.1.jar、xwork-2.0.4.jar
excel导出:jxl.jar
struts.xml;
3.<struts>
4. <package na……
[/Quote]


包都没问题
你这个配置跟我的一样
问题不在这
EverWHL 2012-06-28
  • 打赏
  • 举报
回复
jar全了没
1.所需jar包
struts2需要jar包:commons-logging.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.1.jar、xwork-2.0.4.jar
excel导出:jxl.jar
struts.xml;
3.<struts>
4. <package name="platform-default" extends="struts-default">
5. <action name="excel" class="action.ExcelAction">
6. <result name="excel" type="stream">
7. <param name="contentType">
8. application/vnd.ms-excel
9. </param>
10. <param name="inputName">excelStream</param>
11. <param name="contentDisposition">
12. filename="export.xls"
13. </param>
14. <param name="bufferSize">1024</param>
15. </result>
16. </action>
17. </package>
18.</struts>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧