ireport 和eclipse的整合导出

来源:互联网 发布:淘宝客服kpi考核 编辑:程序博客网 时间:2024/06/09 17:20

public String iReport(){
  
  Map parameters = new HashMap();
  HouseAndHouse info = this.getModel();
//  parameters.put("NAME", "ff");
  
  Connection conn = null;
  ServletContext context = this.getSession().getServletContext();
  String path = context.getRealPath("/") + "WEB-INF/page/houseAndHouse/report1.jasper";
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn
   =DriverManager.getConnection("jdbc:mysql://192.168.1.98:3306/framework?useUnicode=true&characterEncoding=utf8","framework","password");
   File reportFile = new File(path);
     File sourceFile = new File(reportFile.getPath());
           JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);         
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,conn);
           File destFile = new File(context.getRealPath("/") + jasperPrint.getName() + ".html");
           String destFileName = destFile.toString();
          
           JRHtmlExporter exporter = new JRHtmlExporter();
           exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
           exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
           exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
           exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
           exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); //关键是此句      
           exporter.exportReport();
           fileName = jasperPrint.getName() + ".html";
           System.out.println(jasperPrint.getName());
         
           conn.close();
          
 }catch (Exception e) {
  e.printStackTrace();
 } 
  this.getRequest().setAttribute("fileName", fileName);
  return "ireport";
 }

 

这个方法是导出html页面的一个方法

 

public void ireportPDF(){
  Map parameters = new HashMap();
  HouseAndHouse info = this.getModel();
  parameters.put("name", info.getName());
  parameters.put("workUnit",info.getWorkUnit());
  
  Connection conn = null;
  ServletContext context = this.getSession().getServletContext();
  String path = context.getRealPath("/") + "WEB-INF/page/houseAndHouse/report1.jasper";
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn
   =DriverManager.getConnection("jdbc:mysql://192.168.1.98:3306/framework?useUnicode=true&characterEncoding=utf8","framework","password");
   File reportFile = new File(path);
   System.out.println(reportFile.getPath()+"======");
   byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);
//   byte[] bytes = JasperRunManager.runReportToHtmlFile(reportFile.getPath(), parameters, conn);
   this.getResponse().setContentType("application/pdf");
   this.getResponse().setContentLength(bytes.length);
   ServletOutputStream ouputStream = this.getResponse().getOutputStream();
   ouputStream.write(bytes, 0, bytes.length);
   ouputStream.flush();
   ouputStream.close();  
 }catch (Exception e) {
  e.printStackTrace();
 }
 }

这个方法是导出pdf的一个方法。

 

到官方下载了ireport3.6的版本,其实ireport编译用到的包是jasperreports-3.5.2.jar这个版本的,所以在我们的项目中要有这个包才能解析,也就是说ireport里的包和jasperreports-3.5.2.jar这个包的版本要一样才能解析成功,不然会报错。解析不了我们绘制好的test.jasper的报表文件。

在eclipse项目中,我只添加了如下四个包

 

 

原创粉丝点击