后台用的框架,怎么才能用JXL把数据库中的统计日志记录下载保存为EXCEL文件呢。
求高手给个小例子指点下。。
------解决方案--------------------
给你个我现在项目在用的
@SuppressWarnings("unchecked")
@RequestMapping(value = "/huodongbm/export")
public void exportOne(HttpServletRequest request,
HttpServletResponse response, Model model) throws Exception {
List<XsxmModel> list = new ArrayList<XsxmModel>();
Map<String, Object> queryMap = (Map<String, Object>) request.getSession().getAttribute("map");
list = xsxmService.querylistByQueryMap(queryMap);
Map<HuodongModel, List<XsxmModel>> map = new HashMap<HuodongModel, List<XsxmModel>>();
for (XsxmModel xsxm : list) {
HuodongModel hd = xsxm.getHd();
if (map.containsKey(hd)) {
map.get(hd).add(xsxm);
} else {
List<XsxmModel> listtemp = new ArrayList<XsxmModel>();
listtemp.add(xsxm);
map.put(hd, listtemp);
}
}
exportExcel(request, response, map);
}
@SuppressWarnings("unchecked")
private void exportExcel(HttpServletRequest request,
HttpServletResponse response, Map map) {
String[] bt = new String[5];
bt[0] = "活动";
bt[1] = "项目";
bt[2] = "学生";
bt[3] = "报名人";
bt[4] = "报名时间";
// 设置导出excel的相关样式
String fileName = "活动报名汇总.xls";
response.setContentType("application/msexcel");// 定义输出类型
try {
fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");// 解决中文
// 文件名问题
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("content-disposition", "attachment; filename="
+ fileName);// 设定输出文件头
try {
WritableWorkbook wwb;
OutputStream os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("活动报名汇总", 0);
ws.getSettings().setVerticalFreeze(1);// 第一行标题冻结。
ws.getSettings().setVerticalFreeze(2);// 第一行标题冻结。
// 表头样式
WritableCellFormat headFormat = new WritableCellFormat(
new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN));
WritableCellFormat normalFont = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 10));
normalFont.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableCellFormat smallhead = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 14,
WritableFont.BOLD));
smallhead.setAlignment(Alignment.CENTRE);
for (int i = 0; i < bt.length; i++) {
ws.setColumnView(i, 30);
}
ws.addCell(new Label(0, 0, "活动报名汇总", headFormat));// 表头
// 表格标题
for (int i = 0; i < bt.length; i++) {
ws.addCell(new Label(i, 1, bt[i], smallhead));
}
int row = 2;
// 数据区域
ws.setColumnView(1, 15);
Map<HuodongModel, List<XsxmModel>> maptemp = (Map<HuodongModel, List<XsxmModel>>) map;
for (Entry<HuodongModel, List<XsxmModel>> entry : maptemp
.entrySet()) {
HuodongModel hd = entry.getKey();
List<XsxmModel> list = entry.getValue();
ws.mergeCells(0, row, 0, row + list.size() - 1);
ws.addCell(new Label(0, row, hd.getHdmc() + "\r\n"
+ hd.getHdfbsj() + "\r\n" + hd.getHddd(), normalFont));
for (int i = 0; i < list.size(); i++) {
ws.setRowView(row + i, 300);
XsxmModel xsxm = list.get(i);
XiangmuModel xm = xsxm.getXm();
ws.addCell(new Label(1, row + i, xm.getXmmc(), normalFont));
ws
.addCell(new Label(2, row + i, xsxm.getXsgh() + "/"
+ xsxm.getXsxm() + "/" + xsxm.getXsxy(),
normalFont));
ws.addCell(new Label(3, row + i, xsxm.getBmrgh() + "/"
+ xsxm.getBmrxm(), normalFont));
ws
.addCell(new Label(4, row + i, xsxm.getBmsj(),
normalFont));
}
row += list.size();
}
wwb.write();
wwb.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
看着改下 应该会的吧