一、把框架搭好
struts jar
commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.0.4
commons-logging-1.1
freemarker-2.3.13
ognl-2.6.11
struts2-core-2.1.6
xwork-2.1.2
Poi jar:
poi-3.6-20091214
二、写userinfo类
private int id ;
private String name;
private String pass;
private String lastName;
private String address;
private String remark;
以下省略了get&set方法
三、再写导出页面
<body>
<s:form action="outExcel" method="post">
<input type="hidden" name="type" value="xls"/>
<s:submit value="导出数据"></s:submit>
</s:form>
四、把连接数据库和查询数据写出来这部省略
五、写action,如下:
public class OutExcelAction extends ActionSupport implements ServletResponseAware{
private UserInfoDao ui=new UserInfoDao();
private String type="xls";//文件类型
private String fileName;//文件名字
private HttpServletResponse response;
@Override
public String execute() throws Exception {
//响应头
responseHead();
//导出数据
outExcel(response.getOutputStream());
//刷新流
response.getOutputStream().flush();
//关闭流
response.getOutputStream().close();
//这里一定要返回null,不然会报了: java.lang.IllegalStateException: getOutputStream() has already been called for this response的错误.
return null;
}
//设置响应头
public void responseHead(){
try {
//response.setContentType("application/msexcel;charset=UTF-8");
//转换地址的编码
response.setHeader("Content-Disposition", "attachment;filename="
+java.net.URLEncoder.encode(this.fileName, "UTF-8"));
//客户端不缓存
//response.addHeader("Pargam", "no-cache");
//Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache)
//response.addHeader("Cache-Control", "no-cache");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//导出数据
public void outExcel(OutputStream os){
//列名
Workbook book=new HSSFWorkbook();
Sheet sheet=book.createSheet("导出数据");
Row row=sheet.createRow(0);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("用户名");
row.createCell(2).setCellValue("密码");
row.createCell(4).setCellValue("真实姓名");
row.createCell(5).setCellValue("地址");
row.createCell(6).setCellValue("备注");
List<userinfo> list=ui.Allfind();
//把查询出来的数据导到excel里面
for(int i=1;i<=list.size();i++){
userinfo use=list.get(i-1);//把数据遍历,一一拿出来放到excel行中
row=sheet.createRow(i);//行
row.createCell(0).setCellValue(use.getId());
row.createCell(1).setCellValue(use.getName());
row.createCell(2).setCellValue(use.getPass());
row.createCell(3).setCellValue(use.getLastName());
row.createCell(4).setCellValue(use.getAddress());
row.createCell(5).setCellValue(use.getRemark());
}
try {
//把流写到工作簿
book.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}
public UserInfoDao getUi() {
return ui;
}
public void setUi(UserInfoDao ui) {
this.ui = ui;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
this.fileName="userinfo.xls";
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//set方法一定要
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
}
错误总结:
会报: java.lang.IllegalStateException: getOutputStream() has already been called for this response的错误.
引起错误的原因:Struts2方法之间调用引起的。 因为:每个方法都返回的是一个Action对象,而response是Action对象参数,所以就会使response冲突!
解决方法:将最后的return "SUCCESS"改为 return null .不将其交由sturts2管理.就不会报异常了