iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Winform项目中使用FastReport.Net报表控件
  • 200
分享到

Winform项目中使用FastReport.Net报表控件

2024-04-02 19:04:59 200人浏览 泡泡鱼
摘要

目录一、基本使用1、准备工程和引入控件1、下载、安装FastReport2、准备工程、引入控件3、启动页设计2、使用控件搭建窗体1、准备一个FastReport报表2、引入Previ

一、基本使用

1、准备工程和引入控件

1、下载、安装FastReport

这一步很简单,大家在其中文网站上下载最新版的demo版就可以了,直接安装就可以

替换破解文件:

Replace C:\windows\Microsoft.net\assembly\GAC_MSIL\FastReport\v4.0_2019.1.5.0__00000000000000000000000000\FastReport.dll with cracked one.

Assemblies from folders Framework X.0 is PublicKeyToken removed and strong name verification disabled.

安装之后大家会发现,VS里面什么都没有,不像有些插件直接会在ToolBox里显示,这里需要我们自己引入

2、准备工程、引入控件

首先我们使用VS新建一个winform工程,这里我使用的是VisualStutio2015版本

接着我们先引入FastReport的核心dll依赖,这些文件的目录在FastReport安装目录下,分别是FastReport.dll,FastReport.Editor.dll,FastReport.Bars.dll。

你可以使用Framework 4.0下的dll文件

接着我们需要3个窗体:MainFORM,DesignForm,PreviewForm,其中MainForm为启动页面

现在我们需要在ToolsBox中引入我们需要的FastReport控件,首先我们在ToolsBox中新建一个Item,命名为FastReport

然后右键刚刚新建的选项卡->选择项,打开选择控件的对话框

然后我们点击左下角的浏览,选择刚刚的FastReport.dll,然后确定,之后再确定,就会成功导入以下新的控件

3、启动页设计

MainForm很简单,我们就放两个按钮,一个设计,一个浏览,分别打开两个窗口

事件

private void btnDesign_Click(object sender, EventArgs e)
{
    DesignForm dForm = new DesignForm();
    dForm.Show();

}

private void btnPreview_Click(object sender, EventArgs e)
{
    PreviewForm pForm = new PreviewForm();
    pForm.Show();
}

2、使用控件搭建窗体

1、准备一个FastReport报表

使用安装时我们的设计工具设计一张最简单的报表

设计的报表,只有一个文字框

将这份报表保存到工程文件/bin/Debug/Report下

2、引入Preview控件

我们在PreviewForm中,将PreviewControl控件拖入窗体,将窗体拉大一点,然后将控件的Dock设为Fill

然后我们F5测试一下看看是什么效果

我们发现控件被正确的显示出来了

那怎么才能看到我们报表呢,我们需要用代码来加载,我们双击Form,新建Load函数,打下面的代码

using FastReport;//引入FastReport
using System;
using System.Windows.Forms;

namespace ReportDemo
{
    public partial class PreviewForm : Form
    {
        private Report pReport; //新建一个私有变量

        public PreviewForm()
        {
            InitializeComponent();
        }

        private void PreviewForm_Load(object sender, EventArgs e)
        {
            pReport = new Report();   //实例化一个Report报表
            String reportFile = "Report/report.frx";
            pReport.Load(reportFile);  //载入报表文件
            pReport.Preview = previewControl1; //设置报表的Preview控件(这里的previewControl1就是我们之前拖进去的那个)
            pReport.Prepare();   //准备
            pReport.ShowPrepared();  //显示
        }
    }
}

我们再F5一下,载入了报表文件的样子

这里我们已经可以预览我们的报表了 但是在我们的需求中,用户还需要自定义报表的内容和格式呢,我们下一步就在实现报表设计器

3、引入Design控件

我们像Preview那样把Design控件拖进DesignForm,然后Dock设为Fill

然后我们来写怎么样吧设计器绑定Report文件,双击新建Load函数,引入FastReport,新建一个private变量

using FastReport;
using System;
using System.Windows.Forms;

namespace ReportDemo
{
    public partial class DesignForm : Form
    {
        private Report dReport;

        public DesignForm()
        {
            InitializeComponent();
        }

        private void DesignForm_Load(object sender, EventArgs e)
        {
            dReport = new Report();
            string reportFile = "Report/report.frx";
            dReport.Load(reportFile);
            this.designerControl1.Report = dReport;
            dReport.Prepare();
            dReport.Design();
        }
    }
}

我们F5一下

成功!

3、绑定数据

1、数据库准备

我们使用VisualStudio自带的mdf文件数据库,首先我们在工程中创建一个文件夹APP_DATA,在此文件夹中创建一个mdf文件

然后我们可以在服务器资源管理器中看到我们的数据库

然后我们右键表新建一个表

CREATE TABLE [dbo].[T_students]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [no] NCHAR(50) NULL, 
    [name] NCHAR(50) NULL, 
    [school] NCHAR(50) NULL, 
    [class] NCHAR(50) NULL
)

然后在设计器左上角点击更新按钮,在弹出的窗口中点击更新数据库

更状态全部打钩之后,表就创建好了,我们刷新服务器资源管理器,然后打开表数据,添加一些数据进去

ok我们现在在服务器资源管理器里面选择mdf文件,在属性列表里,找到连接字符串,拷贝一份出来,等会需要用的到

Data Source=(LocalDB)\v11.0;AttachDbFilename="D:\Personal\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\APP_DATA\Database1.mdf";Integrated Security=True

2、设计器数据获取

我们在DesignForm.cs里,写一个方法getData()

private DataSet getData()
{
    String connStr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Personal\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\APP_DATA\Database1.mdf;Integrated Security=True";
    sqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    String sqlStr = "SELECT * FROM T_students";
    SqlCommand comm = new SqlCommand();
    comm.CommandText = sqlStr;
    comm.CommandType = CommandType.Text;
    comm.Connection = conn;
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter(comm);
    adapter.Fill(ds, "学生信息");
    conn.Close();
    return ds;
}

然后我们在Form_Load方法里绑定数据集

private void DesignForm_Load(object sender, EventArgs e)
{
    dReport = new Report();
    string reportFile = "Report/report.frx";
    dReport.Load(reportFile);
    this.designerControl1.Report = dReport;

    DataSet ds = new DataSet();
    ds = getData();
    dReport.ReGISterData(ds, "学生信息");
    dReport.Prepare();
    dReport.Design();
}

我们F5一下,在设计窗口下,在[数据]->[选择数据源]中,就能看到我们绑定的数据了

我们设计一个表格,把我们的数据放进去

我们可以预览一下,然后保存

3、为Preview绑定数据

现在我们用同样的方法为Preview绑定数据,getData()方法一致,可以直接复制过来

private void PreviewForm_Load(object sender, EventArgs e)
{
    pReport = new Report();   //实例化一个Report报表
    String reportFile = "Report/report.frx";
    pReport.Load(reportFile);  //载入报表文件
    pReport.Preview = previewControl1; //设置报表的Preview控件(这里的previewControl1就是我们之前拖进去的那个)
    DataSet ds = new DataSet();
    ds = getData();
    pReport.RegisterData(ds, "学生信息");
    pReport.Prepare();   //准备
    pReport.ShowPrepared();  //显示
}

我们测试一下

二、用户自定义报表,可保存到服务器和打开。

摘自官方Demo:

调用设计器界面

首页代码

public partial class Form1 : Form
{
    private DataSet FReportsDs;

    private DataTable ReportsTable
    {
        get { return FReportsDs.Tables[0]; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void InitializeDatabase()
    {
        FReportsDs = new DataSet();
        FReportsDs.ReadXml(Config.ApplicationFolder + @"..\..\database.xml");
    }

    private void FinalizeDatabase()
    {
        FReportsDs.WriteXml(Config.ApplicationFolder + @"..\..\database.xml", XmlWriteMode.WriteSchema);
    }

    private void WireupDesignerEvents()
    {
        Config.DesignerSettings.CustomOpenDialog += new OpenSaveDialogEventHandler(DesignerSettings_CustomOpenDialog);
        Config.DesignerSettings.CustomOpenReport += new OpenSaveReportEventHandler(DesignerSettings_CustomOpenReport);
        Config.DesignerSettings.CustomSaveDialog += new OpenSaveDialogEventHandler(DesignerSettings_CustomSaveDialog);
        Config.DesignerSettings.CustomSaveReport += new OpenSaveReportEventHandler(DesignerSettings_CustomSaveReport);
    }

    private void DesignReport()
    {
        using (Report report = new Report())
        {
            report.LoadBaseReport += new CustomLoadEventHandler(report_LoadBaseReport);
            report.Design();
        }
    }

    // this event is fired when loading a base part of an inherited report.
    private void report_LoadBaseReport(object sender, CustomLoadEventArgs e)
    {
        OpenReport(e.Report, e.FileName);
    }

    // this event is fired when the user press the "Open file" button
    private void DesignerSettings_CustomOpenDialog(object sender, OpenSaveDialogEventArgs e)
    {
        using (OpenDialogForm form = new OpenDialogForm())
        {
            // pass the reports table to display a list of reports
            form.ReportsTable = ReportsTable;

            // show dialog
            e.Cancel = form.ShowDialog() != DialogResult.OK;

            // return the selected report in the e.FileName
            e.FileName = form.ReportName;
        }
    }

    // this event is fired when report needs to be loaded
    private void DesignerSettings_CustomOpenReport(object sender, OpenSaveReportEventArgs e)
    {
        OpenReport(e.Report, e.FileName);
    }

    // this event is fired when the user press the "Save file" button to save untitled report,
    // or "Save file as" button
    private void DesignerSettings_CustomSaveDialog(object sender, OpenSaveDialogEventArgs e)
    {
        using (SaveDialogForm form = new SaveDialogForm())
        {
            // show dialog
            e.Cancel = form.ShowDialog() != DialogResult.OK;

            // return the report name in the e.FileName
            e.FileName = form.ReportName;
        }
    }

    // this event is fired when report needs to be saved
    private void DesignerSettings_CustomSaveReport(object sender, OpenSaveReportEventArgs e)
    {
        SaveReport(e.Report, e.FileName);
    }

    private void OpenReport(Report report, string reportName)
    {
        // find the datarow with specified ReportName
        foreach (DataRow row in ReportsTable.Rows)
        {
            if ((string)row["ReportName"] == reportName)
            {
                // load the report from a stream contained in the "ReportStream" datacolumn
                byte[] reportBytes = (byte[])row["ReportStream"];
                using (MemoryStream stream = new MemoryStream(reportBytes))
                {
                    report.Load(stream);
                }
                return;
            }
        }
    }

    private void SaveReport(Report report, string reportName)
    {
        // find the datarow with specified ReportName
        DataRow reportRow = null;

        foreach (DataRow row in ReportsTable.Rows)
        {
            if ((string)row["ReportName"] == reportName)
            {
                reportRow = row;
                break;
            }
        }

        // no existing row found, append new one
        if (reportRow == null)
        {
            reportRow = ReportsTable.NewRow();
            ReportsTable.Rows.Add(reportRow);
        }

        // save the report to a stream, then put byte[] array to the datarow
        using (MemoryStream stream = new MemoryStream())
        {
            report.Save(stream);

            reportRow["ReportName"] = reportName;
            reportRow["ReportStream"] = stream.ToArray();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        InitializeDatabase();
        WireupDesignerEvents();
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        FinalizeDatabase();
    }

    private void btnDesign_Click(object sender, EventArgs e)
    {
        DesignReport();
    }
}

打开对话框:

public partial class OpenDialogForm : Form
{
    public DataTable ReportsTable
    {
        set
        {
            // fill the listbox with names of reports
            foreach (DataRow row in value.Rows)
            {
                lbxReports.Items.Add(row["ReportName"]);
            }
        }
    }

    public string ReportName
    {
        get
        {
            return (string)lbxReports.SelectedItem;
        }
    }

    public OpenDialogForm()
    {
        InitializeComponent();
    }

    private void lbxReports_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnOK.Enabled = !String.IsNullOrEmpty(ReportName);
    }
}

保存对话框

public partial class SaveDialogForm : Form
{
    public string ReportName
    {
        get
        {
            return tbReportName.Text;
        }
    }

    public SaveDialogForm()
    {
        InitializeComponent();
    }

    private void tbReportName_TextChanged(object sender, EventArgs e)
    {
        btnOK.Enabled = !String.IsNullOrEmpty(ReportName);
    }
}

参考:使用report.ReportResourceString在数据库中保存FastReport.Net报表

https://www.jb51.net/article/250713.htm

控件下载

点此下载

到此这篇关于Winform项目中使用FastReport.Net报表控件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Winform项目中使用FastReport.Net报表控件

本文链接: https://www.lsjlt.com/news/150844.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Winform项目中使用FastReport.Net报表控件
    目录一、基本使用1、准备工程和引入控件1、下载、安装FastReport2、准备工程、引入控件3、启动页设计2、使用控件搭建窗体1、准备一个FastReport报表2、引入Previ...
    99+
    2024-04-02
  • Winform项目中TextBox控件DataBindings属性
    DataBindings属性是很多控件都有的属性,作用有2方面。一方面是用于与数据库的数据进行绑定,进行数据显示。另一方面用于与控件或类的对象进行数据绑定。这里主要关注后者。主要用法...
    99+
    2024-04-02
  • Winform项目中TextBox控件的DataBindings属性怎么用
    本篇内容介绍了“Winform项目中TextBox控件的DataBindings属性怎么用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Da...
    99+
    2023-06-29
  • winform列表控件怎么使用
    WinForm中的列表控件可以使用ListBox、ListView和DataGridView三种控件来实现。1. ListBox控件...
    99+
    2023-09-05
    winform
  • C#中把FastReport.Net报表控件的数据保存到数据库
    通常报表都存储在一个地方,但是在单独的文件中。随着文件数量的增加在结构和搜索上也困难多多,针对这个问题,我遇到了一个非常有趣的报表对象属性——ReportSo...
    99+
    2024-04-02
  • WinForm使用DecExpress控件中的ChartControl插件绘制图表
    目录1.绘制图表基本步骤准备数据并绑定根据数据创建图形展现根据图形对象创建一个图表并绑定到CharControl中调用函数绘制图表2.柱状图准备数据创建图形展现对象方法根据图形对象创...
    99+
    2024-04-02
  • WinForm之中BindingNavigator控件的使用
    BindingNavigator控件是WinForm中的一个导航控件,用于在数据绑定的情况下提供导航和操作数据的功能。它通常与Dat...
    99+
    2023-09-02
    WinForm
  • C#中如何把FastReport.Net报表控件的数据保存到数据库
    这篇“C#中如何把FastReport.Net报表控件的数据保存到数据库”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#中...
    99+
    2023-07-02
  • C#中winform chart控件怎么使用
    在C#中使用WinForms Chart控件,可以按照以下步骤进行操作: 在Visual Studio中创建一个WinForms应...
    99+
    2024-02-29
    C# winform
  • wpf怎么使用winform控件
    WPF(Windows Presentation Foundation)是一种用于构建 Windows 桌面应用程序的技术,而 Wi...
    99+
    2023-09-26
    WPF winform
  • WinForm中怎么使用状态栏控件
    要在WinForm中使用状态栏控件,您可以按照以下步骤操作: 在Visual Studio中打开您的WinForm项目。 在工具箱...
    99+
    2024-03-12
    WinForm
  • WinForm中怎么使用定时器控件
    在WinForm中使用定时器控件非常简单。下面是使用定时器控件的步骤: 在WinForm的工具箱中找到定时器控件(Timer),将...
    99+
    2024-03-12
    WinForm
  • DataGridView控件怎么在C#项目中使用
    DataGridView控件怎么在C#项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1) 创建课程信息表创建课程信息表的 SQL 语句如下。use&n...
    99+
    2023-06-08
  • winform控件之BindingNavigator怎么使用
    WinForm控件之BindingNavigator是用于简化数据绑定操作的控件。它提供了导航、编辑、保存、删除等常用操作的按钮,使...
    99+
    2023-09-29
    winform
  • 1-3 Winform 中的常用控件(
    1-3  Winform 中的常用控件 u     本节学习目标: n System.Windows.Forms.Control基本结构 n 使用基本控件如标签、文本、按钮、列表框和组合框 n 掌握窗...
    99+
    2023-01-31
    控件 常用 Winform
  • WinForm中怎么使用打印对话框控件
    要在WinForm中使用打印对话框控件,可以按照以下步骤进行: 在Visual Studio中打开你的WinForm应用程序项目。...
    99+
    2024-03-12
    WinForm
  • WinForm中如何添加图表或图形控件
    在WinForm中添加图表或图形控件通常使用第三方控件库,比如DevExpress、Telerik等,或者使用.NET Framew...
    99+
    2024-04-08
    winform
  • 1-3 Winform 中的常用控件(3
    8.案例学习:使用组合框控件 本次实验目标是在FORM窗体上建立一个列表框控件,两个组合框控件以及一个文本框控件,通过这些控件彼此之间的关联,学习并掌握ComboBox组合框控件的主要属性和方法。本次实验目标如图1-15所示。&#...
    99+
    2023-01-31
    控件 常用 Winform
  • 1-3 Winform 中的常用控件(2
    4. 案例学习:用户登录功能设计 本次实验目标是通过用户键入名称和密码,经过判别为非空性之后,再判断是否符合系统规定的内容,无论成功或者失败都提示用户操作结果。如图1-12所示为目标界面: ...
    99+
    2023-01-31
    控件 常用 Winform
  • ListView列表组件如何在Android项目中使用
    ListView列表组件如何在Android项目中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。ListView是一种非常常见的一个组件,以垂直列表的形式显示列表项。而...
    99+
    2023-05-31
    android listview roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作