ireport + JasperReport + webwork + ibatis 实现pdf,html,excle报表导出功能
首先利用ireport报表设计工具,生成count.jxml文件,然后编译成.jasper文件,放入项目中
action:
public String generate() throws Exception {
String type = ServletActionContext.getRequest().getParameter("type");
String roomid = ServletActionContext.getRequest().getParameter("id");
String roomname = ServletActionContext.getRequest().getParameter("name");
System.out.println(roomname);
String realPath = ServletActionContext.getServletContext().getRealPath(
"/");
File file = new File(realPath + "/report/Court.jasper");
// String reportFilePath = "D:\\jar\\Court.jasper";
String reportFilePath = file.getPath();
GeneaterService geneater = new GeneaterService();
System.out.println(reportFilePath);
byte[] bytes = null;
if (type.equals("PDF")) {
bytes = geneater.generatePDF(roomid,roomname,reportFilePath);
} else if (type.equals("Excel")) {
bytes = geneater.generateExcel(roomid,roomname,reportFilePath);
} else
bytes = geneater.generateHtml(roomid,roomname,reportFilePath);
HttpServletResponse response = ServletActionContext.getResponse();
ServletOutputStream ouputStream = response.getOutputStream();
if (bytes != null && bytes.length > 0) {
if (type.equals("PDF")) {
response.setContentType("application/pdf");
} else if (type.equals("Excel")) {
response.setContentType("application/vnd.ms-excel");
} else
response.setContentType("text/html");
response.setContentLength(bytes.length);
try {
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} finally {
if (ouputStream != null) {
try {
ouputStream.close();
} catch (IOException ex) {
}
}
}
}
return null; //如果不返回NULL的话,可能会报异常
}
生成报表方法:
/**
* 导出成PDf格式报表
* @param roomid
* @param roomname
* @param reportFilePath
* @return
*/
public byte[] generatePDF(String roomid,String roomname,String reportFilePath) {
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFilePath);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, new HashMap(), new CourtBeanDataSource(roomid,roomname));
return JasperExportManager.exportReportToPdf(jasperPrint);
} catch (JRException e) {
e.printStackTrace();
}
return null;
}
/**
* 导出成HTML格式报表
* @param roomid
* @param roomname
* @param reportFilePath
* @return
*/
public byte[] generateHtml(String roomid,String roomname,String reportFilePath) {
JRHtmlExporter exporter = new JRHtmlExporter();
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFilePath);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, new HashMap(), new CourtBeanDataSource(roomid,roomname));
exporter.setParameter(
JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
exporter
.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter
.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
return bytes;
} catch (JRException e) {
e.printStackTrace();
}
return null;
}
/**
* 导出成excel格式报表
* @param roomid
* @param roomname
* @param reportFilePath
* @return
*/
public byte[] generateExcel(String roomid,String roomname,String reportFilePath) {
JRXlsExporter exporter = new JRXlsExporter(); // Excel
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFilePath);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, new HashMap(), new CourtBeanDataSource(roomid,roomname));
exporter
.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
exporter.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
exporter.setParameter(
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
return bytes;
} catch (JRException e) {
e.printStackTrace();
}
return null;
}
list数据源:
public class CourtBeanDataSource implements JRDataSource {
private int index = -1;
// List<Courtcount> list = Tongji.getCourtcount();
List<Courtcount> list = null;
@SuppressWarnings("unchecked")
public CourtBeanDataSource(String roomid,String roomname) {
list = Tongji.getCourtcountByID(roomid,roomname);
}
public Object getFieldValue(JRField field) throws JRException {
Object value = null;
String fieldName = field.getName();
Courtcount court = list.get(index);
int[] counts = court.getCount();
if("courtName".equals(fieldName)) {
value = court.getCourtName();
}else if("year".equals(fieldName)) {
value = court.getYear();
}
else
for(int i = 1; i <= 12; i++) {
if(("count"+i).equals(fieldName)) {
value = counts[i-1];
}
}
return value;
}
public boolean next() throws JRException {
index ++;
return (index < list.size());
}
}