FastReport.Net的使用

本文详细介绍了如何在FastReport.Net中进行基本使用,包括下载安装、控件引入、启动页设计、数据库准备、数据绑定以及用户自定义报表的保存和预览功能。

FastReport.Net的使用

目录




一、基本使用

1、准备工程和引入控件

1、下载、安装FastReport

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

image

替换破解文件:

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

image

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

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

2、准备工程、引入控件

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

image

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

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

image

image

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

image

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

image

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

image

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

image

3、启动页设计

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

image

事件

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报表

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

image

image

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

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

image

2、引入Preview控件

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

image

然后我们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一下,载入了报表文件的样子

image

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

3、引入Design控件

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

image

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

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一下

image

成功!

3、绑定数据

1、数据库准备

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

image

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

image

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

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
)

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

image

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

image

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

image

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()

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


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;
}

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

image

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

image

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

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();  //显示
}

我们测试一下

image

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值