最近的项目需要对数据进行查询并声称报表打印,也就是把数据库中的字段遍历后输出;
撇开使用其他控件外直接来生成统计报表,一般常用的方法都是直接输出为一个新的htm页面.这个方法的好处是所见即所的。但是缺点是调整格式特别麻烦.需要在dw或frontpage中调后格式后,遍历玩数据后往中写数据。
但是考虑到维护性,而且方便性,可让用户自己调整打印格式,并可下载保存统计报表,最方便的就是生成CSV文件,让用户下载保存,自己排版并打印;代码如下:
//生成日志CSV文件
if (cmd.equals(”CreatelogCsv”)){
String type=request.getParameter(”type”);
response.setContentType(”text/csv”);
response.setHeader(”Cache-Control”, “no-cache”);
response.setCharacterEncoding(”GB2312″);
response.addHeader(”Content-Disposition”, “attachment; filename=\”"+URLDecoder.decode(”report.csv”,”GB2312″)+ “\”");
response.getWriter().write(”操作人员,操作类型,操作内容,访问IP,操作时间\r\n”);
List list =null;
if (type.equals(”syslog2″)){
//系统管理员全部日志
list =logdao.querysyslogbydate(”1000000000″,”",0,0);
}
if (list!=null && list.size()>0){
Iterator it =list.iterator();
while (it.hasNext()){
LogId logidinfo = (LogId) it.next();
response.getWriter().write(URLDecoder.decode(logidinfo.getLogUser(),”GB2312″)+”,”
+URLDecoder.decode(logidinfo.getLogType(),”GB2312″)+”,”
+URLDecoder.decode(logidinfo.getLogContent(),”GB2312″)+”,”
+URLDecoder.decode(logidinfo.getLogIp(),”GB2312″)+”,”
+URLDecoder.decode(logidinfo.getLogData(),”GB2312″)+”\r\n”);
}
}
}