天天看點

JasperReport+ireport實作報表導出功能

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的話,可能會報異常

 }

生成報表方法:

 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;

 }

 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;

 }

 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());

 }

}