Lotus教程、Java教程、Java虚拟机、Java软件综合开发社区

Lotus、Domino、Java、C#、Web、数据库综合开发教程、资料社区

java中报表打印通过生成CSV格式文件来解决



Published by admin on 01月 18, 2008

最近的项目需要对数据进行查询并声称报表打印,也就是把数据库中的字段遍历后输出;

撇开使用其他控件外直接来生成统计报表,一般常用的方法都是直接输出为一个新的htm页面.这个方法的好处是所见即所的。但是缺点是调整格式特别麻烦.需要在dw或frontpage中调后格式后,遍历玩数据后往<td></td>中写数据。

但是考虑到维护性,而且方便性,可让用户自己调整打印格式,并可下载保存统计报表,最方便的就是生成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”);
    }
         }
        
  }



【版权说明】:本网页上有部分内容来源于网上收集,但不能保证资料的完整性和准确性,仅提供参考和学习。如有侵权请立即通知我们,我们将立即删除,谢谢合作!

Add A Comment