- 相關(guān)推薦
怎么用JFreeReport做報表實踐
準(zhǔn)備工作:
下載jfreeReport0.8.7 地址:http://www.jfree.org/jfreereport/index.php
Java認(rèn)證:用JFreeReport做報表實踐,使用JFreeReport生成報表通常需要以下三個基本步驟:
A.生成可通過TableModel接口訪問的數(shù)據(jù),如本例中的createData方法完成的功能。
B.生成一個JFreeReport實例,他定義了我們?nèi)绾胃袷交@示數(shù)據(jù),如本例中的createReportDefinition方法完成的功能。
C.將數(shù)據(jù)和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包中。 內(nèi)容如下:
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.最終出現(xiàn):
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文件。
【怎么用JFreeReport做報表實踐】相關(guān)文章:
hr工作報表怎么做03-08
怎么做冰淇淋03-12
眉筆怎么用03-29
面膜怎么用02-27
丹麥面包怎么做02-14
香蕉奶昔怎么做03-13
意大利面怎么做03-14
酸奶蛋糕怎么做03-12
夏日冷飲怎么做03-11