开发者社区> 问答> 正文

ssh2导出excel?报错

写了一个关于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();
		}
	}
}





展开
收起
爱吃鱼的程序员 2020-06-12 14:05:51 504 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    既然是导出Excel,应该不能用Ajax方式吧。另外“导出”的英文不是import而是exportajax不支持下载文件,使用form表单提交吧。<divclass='ref'>

    引用来自“zigzagroad”的评论

    既然是导出Excel,应该不能用Ajax方式吧。另外“导出”的英文不是import而是export既然都已经提示“数据传输错误”了,那就顺藤摸瓜解决啊

    2020-06-12 14:06:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多