java struts2 JasperReports 生成pdf文件

jasperreport









JAVA生成PDF,最常见的是采用itext组件来动态构建PDF文件,但仅仅用itext生成的PDF,只能靠人为的一点一点来用代码来控制PDF的格式,不能达到可视化的效果。

因此,在这里,我主要介绍的是采用jasperreport模版来定制生成PDF。

1、用ireport定制一个jasperreport的模版

2、用java操作此模版

第1步,这里就不做介绍,很简单的,几乎是所看即所得的视图操作



第2步,大家请看我的java代码,已经测试过,没有问题

注意:下面代码是运行在java1.5或更高版本

需要额外导入jasperreports-3.0.0.jar,itext-1.3.1.jar,iTextAsian.jar三个jar包



package product.file.report.jasperreport;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import product.file.report.ReportGenerater;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.fill.JRFiller;
import net.sf.jasperreports.engine.util.JRLoader;

@SuppressWarnings("unchecked")
public class JasperGenerater implements ReportGenerater{

//生成PDF
public void generatePdf(String template, String output, Map param, Map<String, List<Map>> datasource){
 
  JasperReport jasper = this.loadTemplate(template);     //加载japserreport模版
  JasperPrint jPrint = this.fileReport(jasper, param, datasource); //填充数据
  try {
   JasperExportManager.exportReportToPdfFile(jPrint, output);  //生成PDF文件
  } catch (JRException e) {
   throw new RuntimeException();
  } 
}

//生成Html
public void generateHtml(String template, String output, Map param, Map<String, List<Map>> datasource){
  JasperReport jasper = this.loadTemplate(template);     //加载japserreport模版
  JasperPrint jPrint = this.fileReport(jasper, param, datasource); //填充数据
  try {
   JasperExportManager.exportReportToHtmlFile(jPrint, output);  //生成html文件
  } catch (JRException e) {
   throw new RuntimeException();
  }
}

//生成PDF输出流
public void generatePdf(String template, OutputStream os, Map param, Map<String, List<Map>> datasource){
 
  JasperReport jasper = this.loadTemplate(template);     //加载japserreport模版
  JasperPrint jPrint = this.fileReport(jasper, param, datasource); //填充数据
  try {
   JasperExportManager.exportReportToPdfStream(jPrint, os);  //生成PDF文件流
  } catch (JRException e) {
   throw new RuntimeException();
  } 
}

//加载报表模版
private JasperReport loadTemplate(String template){
  try {
   JasperReport jasper = (JasperReport)JRLoader.loadObject(template);
   return jasper;
  } catch (JRException e) {
   throw new RuntimeException("JRException");
  }
}

//填充报表数据
private JasperPrint fileReport(JasperReport jasper, Map param, Map<String, List<Map>> datasource){
  Set keyset = datasource.keySet();
  Iterator itor = keyset.iterator();
  while(itor.hasNext()){
   String tokey = String.valueOf(itor.next());
   JRDataSource jrDs = new JRMapCollectionDataSource(datasource.get(tokey));
   param.put(tokey, jrDs);
  }
 
  try {
   JasperPrint jPrint = JRFiller.fillReport(jasper, param);
   return jPrint;
  } catch (JRException e) {
   throw new RuntimeException();
  } 
}

//测试Main
public static void main(String[] args){
  JasperGenerater jasperGenerater = new JasperGenerater();
  String template = "D://编程工具//ireport//user.jasper";     //jasper模版
  String outputfile = "D://测试.PDF";          //输出路径
 
  Map<String,List<Map>> datasource = new HashMap<String,List<Map>>();  //数据源;可为多List
  List<Map> list = new ArrayList<Map>();
  for(int i=0; i<10; i++){
   Map cur = new HashMap();
   cur.put("USER_ID", "USER"+i);
   cur.put("USER_NAME", "用户"+i);
   cur.put("LOGIN_COUNT", i);
   list.add(cur);
  }
  datasource.put("REPORT_DATA_SOURCE", list);        //指定数据源

  Map param = new HashMap();            //参数

  param.put("title", "用户登陆统计");
  param.put("date", "2009.01.01——2009.08.08");
  jasperGenerater.generatePdf(template, outputfile, param, datasource); //生成PDF文件
 
}


}

你可能感兴趣的:(jasperreports)