怎么用JFreeReport做报表实践

发布时间:2016-10-20 00:00:00 编辑:嘉辉 手机版

  准备工作:

  下载jfreeReport0.8.7 地址:http://www.jfree.org/jfreereport/index.php

  Java认证:用JFreeReport做报表实践,使用JFreeReport生成报表通常需要以下三个基本步骤:

  A.生成可通过TableModel接口访问的数据,如本例中的createData方法完成的功能。

  B.生成一个JFreeReport实例,他定义了我们如何格式化显示数据,如本例中的createReportDefinition方法完成的功能。

  C.将数据和JFreeReport实例连接起来,并且将该JFreeReport实例传给PreviewDialog的一个实例显示给用户。

  下面开始:

  1.新建一个WEB工程,在WebRoot\lib中import 如下jar文件:jcommon-1.0.1.jar jfreereport-0.8.7.jar jfreereport-demo-0.8.7.jar jfreereport-servlet-demo-0.8.7.jar

  (可在jfreereport包中解压得到)

  2.写一个Demo.java 加入到工程的src包中。 内容如下:

  import java.awt.Color;

  import java.awt.geom.Point2D;

  import javax.swing.table.DefaultTableModel;

  import javax.swing.table.TableModel;

  import org.jfree.report.ElementAlignment;

  import org.jfree.report.JFreeReport;

  import org.jfree.report.JFreeReportBoot;

  import org.jfree.report.ReportProcessingException;

  import org.jfree.report.demo.helper.DefaultCloseHandler;

  import org.jfree.report.demo.helper.DemoHandler;

  import org.jfree.report.demo.helper.PreviewHandler;

  import org.jfree.report.elementfactory.TextFieldElementFactory;

  import org.jfree.report.modules.gui.base.PreviewDialog;

  import org.jfree.ui.FloatDimension;

  import org.jfree.util.Log;

  public class Demo implements DemoHandler

  {

  private class HelloWorldPreviewHandler implements PreviewHandler

  {

  public HelloWorldPreviewHandler()

  {

  }

  public void attemptPreview()

  {

  executeReport();

  }

  }

  public Demo (){

  }

  protected void executeReport ()

  {

  final TableModel data = createData();

  final JFreeReport report = createReportDefinition();

  report.setData(data);

  try

  {

  final PreviewDialog preview = new PreviewDialog(report);

  preview.addWindowListener(new DefaultCloseHandler());

  preview.pack();

  preview.setVisible(true);

  }

  catch (ReportProcessingException e)

  {

  Log.error(“Failed to generate report ”, e);

  }

  }

  private TableModel createData ()

  {

  final Object[] columnNames = new String[]{“Column1”, “Column2”};

  final DefaultTableModel result = new DefaultTableModel(columnNames, 101);

  for (int i=1;i《101;i++){

  result.setValueAt(“用繁體字寫幾個看看\n”, i, 0);

  result.setValueAt(“简体中文输出第”+i+“次”, i, 1);

  }

  return result;

  }

  private JFreeReport createReportDefinition ()

  {

  final JFreeReport report = new JFreeReport();

  report.setName(getDescription());

  TextFieldElementFactory factory = new TextFieldElementFactory();

  factory.setName(“T1”);

  factory.setAbsolutePosition(new Point2D.Float(0, 0));

  factory.setMinimumSize(new FloatDimension(150, 12));

  factory.setColor(Color.black);

  factory.setHorizontalAlignment(ElementAlignment.RIGHT);

  factory.setVerticalAlignment(ElementAlignment.MIDDLE);

  factory.setNullString(“-”);

  factory.setFieldname(“Column1”);

  report.getItemBand()。addElement(factory.createElement());

  factory = new TextFieldElementFactory();

  factory.setName(“T2”);

  factory.setAbsolutePosition(new Point2D.Float(200, 0));

  factory.setMinimumSize(new FloatDimension(150, 12));

  factory.setColor(Color.black);

  factory.setHorizontalAlignment(ElementAlignment.LEFT);

  factory.setVerticalAlignment(ElementAlignment.MIDDLE);

  factory.setNullString(“-”);

  factory.setFieldname(“Column2”);

  report.getItemBand()。addElement(factory.createElement());

  return report;

  }

  protected String getDescription ()

  {

  return “A Very Simple Report”;

  }

  public String getDemoName()

  {

  return “Hello World Demo (External)”;

  }

  public PreviewHandler getPreviewHandler()

  {

  return new HelloWorldPreviewHandler();

  }

  public static void main (final String[] args)

  {

  JFreeReportBoot.getInstance()。start();

  new Demo()。executeReport();

  }

  }

  3.在eclipse 中运行(Shift+Alt+X J)运行出来了。 仍有错误或警告。下面来消除它们。

  4.Exception in thread “export-worker——1” java.lang.NoClassDefFoundError: org/jfree/fonts/registry/FontRecord === 解决办法是 import libfonts-0.1.4.jar

  5.WARN: Unable to initialize the module misc-bsf

  org.jfree.base.modules.ModuleInitializeException: Unable to load the bean scripting framework manager class. This class is required to execute the BSFExpressions. === 解决办法是 import bsf-2.3.0.jar

  6.WARN: Unable to initialize the module misc-beanshellorg.jfree.base.modules.ModuleInitializeException: Unable to load the bean shell interpreter class. This class is required to execute the BeanShellExpressions. === 解决办法是 import bsh-1.3.0.jar

  7.WARN: Unable to load module classes for parser-simple:org/jfree/xml/parser/AbstractXmlReadHandler

  WARN:Unable to load module classes for parser-ext:org/jfree/xml/parser/AbstractXmlReadHandler

  === 解决办法是 import jcommon-xml-1.0.1.jar

  8.最终出现:

  INFO:Registering fonts for the iText library; using a cached font registry.

  INFO:Completed font registration.

  INFO:setReport(……): started pagination .……

  OK,一切顺利完成。

  9.总之最后的WebRoot\lib中一共引入了

  bsf-2.3.0.jar bsh-1.3.0.jar itext-1.3.6.jar jcommon-1.0.1.jar jfreereport-0.8.7.jar jfreereport-demo-0.8.7.jar jfreereport-servlet-demo-0.8.7.jar

  libfonts-0.1.4.jar poi-3.0-alpha1-20050704.jar

  这些JAR文件。

本文已影响870
+1
0