写了一个关于struts2导出excel的方法,后台为报错,但找不到相应的文件,不知是哪里错了,求指教?
前台
/* 导出excel */ var importExcel = function(){ $.ajax({ type:"POST", url:"${pageContext.request.contextPath }/importExcel", dataType:"html", success:function(data){ confirm("导出成功"); } }); }
后台
struts.xml
<!-- 导出excel --> <action name="importExcel" class="org.fkjava.s2sh.action.ImportExcelAction"> <result name="excel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">excelInputStream</param> <param name="contentDisposition">filename="用户情况详情.xls"</param> <param name="bufferSize">4096</param> </result> </action>
action
package org.fkjava.s2sh.action; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URLEncoder; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.fkjava.s2sh.domain.User; import org.fkjava.s2sh.service.UserService; import org.fkjava.s2sh.util.ExcelUtil; import com.opensymphony.xwork2.Action; public class ImportExcelAction implements Action { private UserService us; private List<User> users; private InputStream excelInputStream; public List<User> getUsers() { return users; } public void setUsres(List<User> users) { this.users = users; } public void setUs(UserService us) { this.us = us; } public InputStream getExcelInputStream() { users = us.getAllUser(null); ByteArrayOutputStream out = new ByteArrayOutputStream(); ExcelUtil.toExcel(users, out); return new ByteArrayInputStream(out.toByteArray()); } @Override public String execute() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户情况详情", "UTF-8") + ".xls"); excelInputStream = this.getExcelInputStream(); return "excel"; } }excel生成类
package org.fkjava.s2sh.util; import java.io.ByteArrayOutputStream; import java.util.List; import org.fkjava.s2sh.domain.User; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelUtil { public static void toExcel(List<User> users, ByteArrayOutputStream os) { String[] title = { "姓名", "密码", "生日" }; WritableWorkbook wwb = null; try { wwb = Workbook.createWorkbook(os); WritableSheet sheet = wwb.createSheet("用户信息详情.xls", 0); Label label = null; for (int i = 0; i < title.length; i++) { label = new Label(i, 0, title[i]); sheet.addCell(label); } Label label2 = null; User user = null; for (int i = 0; i < users.size(); i++) { user = users.get(i); label2 = new Label(0, i + 1, String.valueOf(user.getName())); sheet.addCell(label2); label2 = new Label(1, i + 1, String.valueOf(user.getPass())); sheet.addCell(label2); label2 = new Label(2, i + 1, String.valueOf(user.getBirth())); sheet.addCell(label2); } wwb.write(); } catch (Exception e) { e.printStackTrace(); } } }
既然是导出Excel,应该不能用Ajax方式吧。另外“导出”的英文不是import而是exportajax不支持下载文件,使用form表单提交吧。<divclass='ref'>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。