用jxl做了个导出excel
我的导出按钮所在的页面,被包含在一个iframe中.我的导出是直接导出的.没有事先保存到服务器上.所以打开的时候会提示"您尝试打开的文件'xxx.action'的格式与文件扩展名指定的格式不一致....."
在IE中 点击导出按钮,选择保存,这个很正常
但是选择打开,也能打开.但是第一次打开之后,打开的excel不关闭,再点 导出按钮,然后在选择打开,打开的excel就是空白的
在遨游3中 点击导出按钮,选择保存,这个很正常
但是选择打开,也能打开.但是第一次打开之后,打开的excel不关闭,再点 导出按钮,然后在选择打开,这个时候,整个web程序就好像死掉了一样,点哪里都没有反映了,就被卡死了一样
如果把那个下载的页面直接从浏览器打开,而不被包含在iframe中,就什么问题也没有
有人知道应该如何配置吗?
我的struts.xml中下载配置如下
- XML code
<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>
------解决方案--------------------------------------------------------
- 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.jarexcel导出:jxl.jarstruts.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>
------解决方案--------------------------------------------------------
导出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 : "";