日期:2014-05-17  浏览次数:20732 次

在Java Web开发时,如何将页面上的数据用Excel报表导出来
求回复~~~~
  我用的是ssh框架做的项目

------解决方案--------------------
http://www.javaeye.com/topic/705178
------解决方案--------------------
this.getRes().reset();
this.getRes().setContentType("application/vnd.ms-excel;charset=gb2312");
this.getRes().setHeader("Content-Disposition", "attachment; filename=\""
+ new String("物资月度中标金额比较表.xls".getBytes("gb2312"),"ISO-8859-1") + "\"");
OutputStream output = this.getRes().getOutputStream();
ExcelWorkBook workBook = ExcelUtil.getWorkBook(output);
ExcelSheet sheet = workBook.addSheet("sheet1");

sql 得到list
for(){
遍历list
插入到对应的cell中
}
workBook.write(output);
output.flush();
output.close();
------解决方案--------------------
写一个action(A)用于从数据库取出数据,写一个页面(action A返回的页面)excel.jsp
excel.jsp页面做成需要导出的excel的格式
对excel.jsp做特殊处理:

<%
//就是靠这一行,让前端浏览器以为接收到一个excel档
response.setHeader("Content-disposition","attachment; filename="+request.getAttribute("startDate")+"~"+request.getAttribute("endDate")+".xls");
%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<head>
<title></title>
</head>
<body>
<table>
.....
</table>
</body>
</html>

这样就OK啦