<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>导出excel表格</title>
<script type="text/javascript">
function hpExport(obj) {
obj.href = "excelExport.jsp";
return true;
}
</script>
</head>
<body>
<a href="#" id="hpExport" onClick="return hpExport(this)">导出数据</a>
</body>
</html>
3.新建导出excel表格操作页面excelExport.jsp
<%@ page language="java"
import="java.util.*,java.io.*,jxl.write.WritableWorkbook,jxl.Workbook,jxl.write.WritableSheet,jxl.write.Label,com.bestnet.dao.HotDao,com.bestnet.dao.HotVo"
pageEncoding="GBK"%>
<%
try {
String city = request.getParameter("city");
String grade = request.getParameter("grade");
response.setContentType("application/vnd.ms-excel"); //保证不乱码
String fileName = city + "excel表格数据.xls";
//response.addHeader("Content-Disposition","attachment; filename="+city+excel表格数据.xls","ISO-8859-1");
response.setHeader("Content-Disposition", "attachment;"
+ " filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
OutputStream os = response.getOutputStream();
out.clear();
out = pageContext.pushBody(); //这2句一定要,不然会报错。
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet(city, 0);
HotDao hotdao = new HotDao();
List hotlist = new ArrayList();
if (Integer.parseInt(grade) == 2) {
hotlist = hotdao.list();
} else if (Integer.parseInt(grade) == 3) {
hotlist = hotdao.citylist(city);
} else if (Integer.parseInt(grade) == 4) {
hotlist = hotdao.subcitylist(city);
}
Label label = new Label(0, 0, "热点名称");
ws.addCell(label);
label = new Label(1, 0, "热点地址");
ws.addCell(label);
label = new Label(2, 0, "覆盖范围");
ws.addCell(label);
for (int i = 0; i < hotlist.size(); i++) {
HotVo hotvo = (HotVo) hotlist.get(i);
label = new Label(0, (i + 1), hotvo.getHOTNAME());
ws.addCell(label);
label = new Label(1, (i + 1), hotvo.getADDRESS());
ws.addCell(label);
label = new Label(2, (i + 1), hotvo.getAREA());
ws.addCell(label);
System.out.println(hotvo.getHOTNAME()
+ "+++++++++++++++++++++++++++++++++++++++++++++");
}
wwb.write();
wwb.close();
os.close();
} catch (Exception e) {
System.out.println("生成信息表(Excel格式)时出错:");
e.printStackTrace();
}
%>