欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JasperReport与hibernate结合使用 HibernateExcelServletWebXML 

程序员文章站 2022-03-11 10:34:35
...
花了两天时间,终于把jasperreport与项目中使用的hibernate结合使用.最新版本的ireport支持HQL查询,可以在 ireport里面写HQL语句查询并设计好报表.需要注意的是把hibernate.cfg.xml包括进classpath!!(菜单options ->classpath进行设置)给张图:

JasperReport与hibernate结合使用
            
    
    
        HibernateExcelServletWebXML 

设计好报表之后编译成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();
   }