花了兩天時(shí)間,終于把jasperreport與項(xiàng)目中使用的hibernate結(jié)合使用.最新版本的ireport支持HQL查詢,可以在 ireport里面寫(xiě)HQL語(yǔ)句查詢并設(shè)計(jì)好報(bào)表.需要注意的是把hibernate.cfg.xml包括進(jìn)classpath!!(菜單options ->classpath進(jìn)行設(shè)置)給張圖:

設(shè)計(jì)好報(bào)表之后編譯成jasper文件,在servlet中調(diào)用,我們需要實(shí)現(xiàn)自己的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;
?}
}
此類構(gòu)造函數(shù)需要兩個(gè)參數(shù),查詢結(jié)果的list以及查詢的所有屬性名(這些屬性名必須和報(bào)表界面的field名一致)
,使用方式如下:
1.導(dǎo)出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.導(dǎo)出excel,需要把poi-2.0-final-20040126.jar放進(jìn)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();
???}