struts2配置: <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没有报错,就是页面不提示下载,显示乱码,头都大了,另一个页面同样配置都可以正常导出 |
|
10分 |
action的method少了指定下<action name=”exportExcel” class=”exportExcel” method=”exportExcel”>
|
15分 |
先要排除Excel有没有生成,或者你配置里面出现了重复转向,
页面弹出下载代码贴出来看看 |
人品!
|
|
我在请求的时候用的是exportExcel!exportExecl.action |
|
excel直接写到硬盘没问题,向前台传的时候不行,struts2配置下载在前台不用写弹出代码,配置了<param name=”contentDisposition”>attachment;filename=”${filename}”</param><!– 下载文件名 –> 就好了 |
|
|
|
10分 |
getInputStream()方法有了吗
|
都有了 |
|
10分 |
this.filename = new Date().toString() + ".xls"; 难道文件名的问题?猜测 |
也不是,这个也试过了 |
|
不要沉了啊。。。。。。。
|
|
15分 |
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; 最后配置文件 |
15分 |
<param name=”contentType”>application/application/vnd.ms-excel</param>
这个改成excel的mimeType再把filename用个简单点的a.xls先试下能下载不 |
15分 |
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的写法 |
试了下,还是不行
|
|
楼主页面上怎么写的? |
|
就一个按钮,请求这个action所有查询sql都是在action拼接出来的 |
|
10分 |
book.write();
book.close(); this.filename = new Date().toString() + “.xls”; this.inputStream = new ByteArrayInputStream(os.toByteArray()); 这段代码看上去不太像是将excel发送到前台的,os和book是怎么关联的,os又是怎么发送到前台的 |
请问楼主解决了没有
|
|
我和楼主遇到一样的问题了,我解决办法是由原来的post异步访问后台的导出方法修改为get方式就没问题了
|
|
public String getFileName() throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes(), “ISO-8859-1”); return fileName; } |