準備工作:
下載jfreeReport0.8.7 地址:
Java認證:用JFreeReport做報表實踐,使用JFreeReport生成報表通常需要以下三個基本步驟:
A.生成可通過TableModel介面訪問的資料,如本例中的createData方法完成的功能。
B.生成一個JFreeReport例項,他定義了我們如何格式化顯示資料,如本例中的'createReportDefinition方法完成的功能。
C.將資料和JFreeReport例項連線起來,並且將該JFreeReport例項傳給PreviewDialog的一個例項顯示給使用者。
下面開始:
1.新建一個WEB工程,在WebRootlib中import 如下jar檔案:
(可在jfreereport包中解壓得到)
2.寫一個 加入到工程的src包中。 內容如下:
import r;
import t2D;
import ultTableModel;
import eModel;
import entAlignment;
import eReport;
import eReportBoot;
import rtProcessingException;
import ultCloseHandler;
import Handler;
import iewHandler;
import FieldElementFactory;
import iewDialog;
import tDimension;
import ;
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();
ata(data);
try
{
final PreviewDialog preview = new PreviewDialog(report);
indowListener(new DefaultCloseHandler());
();
isible(true);
}
catch (ReportProcessingException e)
{
r(“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++){
alueAt(“用繁體字寫幾個看看n”, i, 0);
alueAt(“簡體中文輸出第”+i+“次”, i, 1);
}
return result;
}
private JFreeReport createReportDefinition ()
{
final JFreeReport report = new JFreeReport();
ame(getDescription());
TextFieldElementFactory factory = new TextFieldElementFactory();
ame(“T1”);
bsolutePosition(new t(0, 0));
inimumSize(new FloatDimension(150, 12));
olor(k);
orizontalAlignment(T);
erticalAlignment(LE);
ullString(“-”);
ieldname(“Column1”);
temBand()。addElement(teElement());
factory = new TextFieldElementFactory();
ame(“T2”);
bsolutePosition(new t(200, 0));
inimumSize(new FloatDimension(150, 12));
olor(k);
orizontalAlignment();
erticalAlignment(LE);
ullString(“-”);
ieldname(“Column2”);
temBand()。addElement(teElement());
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)
{
nstance()。start();
new Demo()。executeReport();
}
}
3.在eclipse 中執行(Shift+Alt+X J)執行出來了。 仍有錯誤或警告。下面來消除它們。
ption in thread “export-worker——1” assDefFoundError: org/jfree/fonts/registry/FontRecord === 解決辦法是 import
: Unable to initialize the module misc-bsf
leInitializeException: Unable to load the bean scripting framework manager class. This class is required to execute the BSFExpressions. === 解決辦法是 import
: Unable to initialize the module leInitializeException: Unable to load the bean shell interpreter class. This class is required to execute the BeanShellExpressions. === 解決辦法是 import
: 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
8.最終出現:
INFO:Registering fonts for the iText library; using a cached font registry.
INFO:Completed font registration.
INFO:setReport(……): started pagination .……
OK,一切順利完成。
9.總之最後的WebRootlib中一共引入了
這些JAR檔案。