日期:2014-05-19  浏览次数:20776 次

web项目下载excel会直接在浏览器中打开
因为项目需要,客户端在提交一个请求后,在服务器端生成相应的excel文件,然后通过
response.sendRedirect(filename);
通过重定向将excel下载到客户端,但是excel会直接在浏览器中打开,没有提示保存
怎么办 谢谢

------解决方案--------------------
简单啊这问题
在对应的配置文件中找到对应的Action
<action name="" class="">
<result type="stream">
<param name="contentDisposition">attachment;filename="test.txt"</param>
<param name="inputName">downloadFile</param>
</result>
</action>

有了这行代码,浏览器就不会直接打开了
------解决方案--------------------
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=fine.xls");
response.setContentType("application/msexcel");
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(os);
} catch (IOException e1) {
e1.printStackTrace();
}
WritableFont font3 = new WritableFont(WritableFont.ARIAL, 9,
WritableFont.NO_BOLD, false);
bodyFormat = new WritableCellFormat(font3);
try {
bodyFormat.setBackground(Colour.LIGHT_GREEN);
bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
} catch (WriteException e1) {
e1.printStackTrace();
}
这个代码是在servlet里面的,不知道对于你合不合适??