如何使用 DynamicReports 生成动态报告?

发布于 2024-12-12 03:05:44 字数 6868 浏览 2 评论 0原文

我尝试使用 DynamicReports 生成动态报告,但总是遇到以下异常,任何人都可以给我解决方案。

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: net.sf.jasperreports.engine.base.JRBaseStyle.getParagraph()Lnet/sf/jasperreports/engine/JRParagraph;
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.abstractStyle(StyleTransform.java:116)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.style(StyleTransform.java:74)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.addStyle(StyleTransform.java:65)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.transform(StyleTransform.java:59)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.transform(JasperReportDesign.java:104)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:77)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:69)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:215)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:242)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:252)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperReportBuilder.java:281)
    at ClsManufac.ClsDynamicReport.build(ClsDynamicReport.java:43)
    at ClsManufac.ClsDynamicReport.<init>(ClsDynamicReport.java:30)
    at Manufacturing.FrmMonthlyStock.jButton1ActionPerformed(FrmMonthlyStock.java:335)
    at Manufacturing.FrmMonthlyStock.access$400(FrmMonthlyStock.java:23)
    at Manufacturing.FrmMonthlyStock$5.actionPerformed(FrmMonthlyStock.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6175)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5940)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4536)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

我的 JFrame 中有一个 JTable 和一个 JButton,我需要的是使用 DynamicReport 生成报告(http://dynamicreports .sourceforge.net/)单击按钮时从 JTable 的数据中获取。 单击按钮时我所做的是将表模型传递给以下类,并尝试循环遍历表模型以生成报告。

package com;


import java.awt.Color;

import java.awt.Font;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import java.math.BigDecimal;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import net.sf.dynamicreports.examples.DataSource;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.LineStyle;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.collections.ReferenceMap;
import org.apache.commons.digester.Rule;

public class SimpleReport {

    DefaultTableModel model;

    public SimpleReport(DefaultTableModel model) {
        this.model = model;
        build();
    }

    private void build() {

        StyleBuilder boldStyle = stl.style().bold();
        StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle);
        StyleBuilder titleStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);

        try {
            JasperReportBuilder report = DynamicReports.report();
            report()//create new report design
                    .setColumnTitleStyle(columnTitleStyle).columns(//add columns
                    //            title,     field name     data type
                    //col.columnRowNumberColumn("No").setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("ID", "ID", type.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    col.column("Name", "Name", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("Phone", "Phone", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER)).title(cmp.text("Getting started\n").setStyle(titleStyle))//shows report title
                    .pageFooter(cmp.pageXofY())//shows number of page at page footer
                    .setDataSource(createDataSource())//set datasource
                    .show();//create and show report
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JRDataSource createDataSource() {
        DataSource dataSource = new DataSource("ID", "Name", "Phone");



        for (int i = 0; i < model.getRowCount(); i++) {
            System.out.println(model.getValueAt(i, 2));
            dataSource.add(model.getValueAt(i, 0), model.getValueAt(i, 1), model.getValueAt(i, 2));

        }

        return dataSource;
    }

}

I have tried to generate a dynamic report using DynamicReports but always I am getting following exception,can anyone give me a solution.

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: net.sf.jasperreports.engine.base.JRBaseStyle.getParagraph()Lnet/sf/jasperreports/engine/JRParagraph;
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.abstractStyle(StyleTransform.java:116)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.style(StyleTransform.java:74)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.addStyle(StyleTransform.java:65)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.transform(StyleTransform.java:59)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.transform(JasperReportDesign.java:104)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:77)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:69)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:215)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:242)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:252)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperReportBuilder.java:281)
    at ClsManufac.ClsDynamicReport.build(ClsDynamicReport.java:43)
    at ClsManufac.ClsDynamicReport.<init>(ClsDynamicReport.java:30)
    at Manufacturing.FrmMonthlyStock.jButton1ActionPerformed(FrmMonthlyStock.java:335)
    at Manufacturing.FrmMonthlyStock.access$400(FrmMonthlyStock.java:23)
    at Manufacturing.FrmMonthlyStock$5.actionPerformed(FrmMonthlyStock.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6175)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5940)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4536)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I have a JTable and a JButton in my JFrame, what I need is to generate a report using DynamicReport (http://dynamicreports.sourceforge.net/) from the data of the JTable when click the button.
What I do when clicking the button is, passing the table model to the following class, and try to loop through the table model to generate the report.

package com;


import java.awt.Color;

import java.awt.Font;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import java.math.BigDecimal;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import net.sf.dynamicreports.examples.DataSource;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.LineStyle;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.collections.ReferenceMap;
import org.apache.commons.digester.Rule;

public class SimpleReport {

    DefaultTableModel model;

    public SimpleReport(DefaultTableModel model) {
        this.model = model;
        build();
    }

    private void build() {

        StyleBuilder boldStyle = stl.style().bold();
        StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle);
        StyleBuilder titleStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);

        try {
            JasperReportBuilder report = DynamicReports.report();
            report()//create new report design
                    .setColumnTitleStyle(columnTitleStyle).columns(//add columns
                    //            title,     field name     data type
                    //col.columnRowNumberColumn("No").setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("ID", "ID", type.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    col.column("Name", "Name", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("Phone", "Phone", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER)).title(cmp.text("Getting started\n").setStyle(titleStyle))//shows report title
                    .pageFooter(cmp.pageXofY())//shows number of page at page footer
                    .setDataSource(createDataSource())//set datasource
                    .show();//create and show report
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JRDataSource createDataSource() {
        DataSource dataSource = new DataSource("ID", "Name", "Phone");



        for (int i = 0; i < model.getRowCount(); i++) {
            System.out.println(model.getValueAt(i, 2));
            dataSource.add(model.getValueAt(i, 0), model.getValueAt(i, 1), model.getValueAt(i, 2));

        }

        return dataSource;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-12-19 03:05:44

DynamicReports 似乎使用 JasperReports,但您的类路径中可能有两个库的不兼容版本。

检查您的 DynamicReports 版本的文档,了解所需的 JasperReports 版本。

DynamicReports seems to use JasperReports, but you probably have incompatible versions of both libraries in your classpath.

Check the documentation of your version of DynamicReports to know which version of JasperReports it expects.

﹉夏雨初晴づ 2024-12-19 03:05:44

我认为您无法正确添加 jar,这就是发生错误的原因。首先正确添加 jar,然后检查它是否可以正常工作!

I think you can't add jars properly that's why the errors are occurred.firstly add jars properly and then check it will works fin!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文