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();
}
上一篇: python 基于wx实现音乐播放
推荐阅读
-
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
-
详解AngularJs与SpringMVC简单结合使用
-
mybatis-4 mybatis与spring结合使用及原理解析
-
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
-
详解AngularJs与SpringMVC简单结合使用
-
vue cli3.0结合echarts3.0与地图的使用方法示例
-
vue2.0 与 bootstrap datetimepicker的结合使用实例
-
JavaScript与HTML结合的基本使用方法整理
-
浅要分析Python程序与C程序的结合使用
-
socket io与vue-cli的结合使用的示例代码