庄周梦蝶

生活、程序、未来
   :: 首页 ::  ::  :: 聚合  :: 管理

JasperReport与hibernate结合使用

Posted on 2007-02-06 12:31 dennis 阅读(3095) 评论(1)  编辑  收藏 所属分类: java
花了两天时间,终于把jasperreport与项目中使用的hibernate结合使用.最新版本的ireport支持HQL查询,可以在 ireport里面写HQL语句查询并设计好报表.需要注意的是把hibernate.cfg.xml包括进classpath!!(菜单options ->classpath进行设置)给张图:

test2.jpg

设计好报表之后编译成jasper文件,在servlet中调用,我们需要实现自己的hibernate datasource:

public class HibernateQueryResultDataSource implements JRDataSource {

 private String[] fields;

 private Iterator iterator;

 private Object currentValue;

 public HibernateQueryResultDataSource(List list, String[] fields) {
  this.fields = fields;
  this.iterator = list.iterator();
 }

 public Object getFieldValue(JRField field) throws JRException {
  Object value = null;
  int index = getFieldIndex(field.getName());
  if (index > -1) {
   Object[] values = (Object[]) currentValue;
   value = values[index];
  }
  return value;
 }

 public boolean next() throws JRException {
  currentValue = iterator.hasNext() ? iterator.next() : null;
  return (currentValue != null);
 }

 private int getFieldIndex(String field) {
  int index = -1;
  for (int i = 0; i < fields.length; i++) {
   if (fields[i].equals(field)) {
    index = i;
    break;
   }
  }
  return index;
 }

}

此类构造函数需要两个参数,查询结果的list以及查询的所有属性名(这些属性名必须和报表界面的field名一致)

,使用方式如下:

1.导出pdf:

File reportFile = new File(getServletContext().getRealPath(
    "/reports/" + queryResult.getReportFileName() + ".jasper"));

  HibernateQueryResultDataSource dataSource = new HibernateQueryResultDataSource(
    queryResult.getList(parameters), queryResult.getFields());
  if (reportType.equals("pdf") || reportType.equals("")) {
   try {
    byte[] bytes = JasperRunManager.runReportToPdf(reportFile
      .getPath(), null, dataSource);
    response.setContentType("application/pdf");
    response.setContentLength(bytes.length);
    ServletOutputStream ouputStream = response.getOutputStream();
    ouputStream.write(bytes, 0, bytes.length);
    ouputStream.flush();
    ouputStream.close();

   } catch (Exception e) {
    e.printStackTrace();
   }
  }

2.导出excel,需要把poi-2.0-final-20040126.jar放进web的lib里:

try {

    JasperPrint print = JasperFillManager.fillReport(
      new FileInputStream(reportFile), null, dataSource);
    // JRXlsExporter t=null;
    JRXlsExporter jRXlsExporter = new JRXlsExporter();
    ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
    jRXlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,
      print);
    jRXlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
      xlsReport);
    jRXlsExporter
      .setParameter(
        JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
        Boolean.TRUE);
    jRXlsExporter.setParameter(
      JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
      Boolean.TRUE);
    // jRXlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,ReportsConstants.bankDetailsHtml);

    // jRXlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
    // Boolean.TRUE);

    jRXlsExporter.exportReport();
    byte[] bytes = xlsReport.toByteArray();

   
    response.setContentType("application/vnd.ms-excel");
    response.setContentLength(bytes.length);
    xlsReport.close();
    OutputStream ouputStream = response.getOutputStream();
    ouputStream.write(bytes, 0, bytes.length);
    ouputStream.flush();
    ouputStream.close();

   } catch (Exception e) {
    e.printStackTrace();
   }


评论

# re: JasperReport与hibernate结合使用  回复  更多评论   

2008-05-26 09:41 by wanglijun
我不知道那里配置问题的文件,在iReport使终找不到Hibarnate的映射文件。能不能给我一点资料。我的E-mail是:czwangli88@126.com。Thank you

只有注册用户登录后才能发表评论。


网站导航: