报表生成器成功案例:C#调用FastReport控件示例

FastReport .Net是适用于Windows Forms,ASP.NET,MVC和.NET Core的全功能报表解决方案。它可以在Microsoft Visual Studio 2005-2019中使用。支持.Net Framework 2.0-4.x,.NET Core 3.0及以上版本。

在FastReport .NET 2021.1的新版本中,我们实现了对.NET 5的支持。添加了新条形码-Deutsce Post Leitcode。将RTF转换为报告对象的算法已得到显着改进。并且还添加了用于转换数字的新功能。欢迎下载体验。

【慧都网】下载FastReport.NET v2021.1最新版

Fastreport.NET在线购买价更低,专享85折起!赶紧加入购物清单吧!

下载FastReport组件必须的dll文件,如下图:
报表生成器成功案例:C#调用FastReport控件示例

创建个WinForm项目
报表生成器成功案例:C#调用FastReport控件示例

引用dll文件

报表生成器成功案例:C#调用FastReport控件示例

引用dll文件创建FastReport控件工具
报表生成器成功案例:C#调用FastReport控件示例

创建打印设置From

报表生成器成功案例:C#调用FastReport控件示例
C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using System.Data.SqlClient;

namespace PrintTest001
{
public partial class FrmPrintDesigner : Form
{
public FrmPrintDesigner()
{
InitializeComponent();
}

    private void FrmPrintDesigner_Load(object sender, EventArgs e)
    {
        Report dReport = new Report();   //实例化一个Report报表
        String reportFile = "Report/Report01.frx";
        dReport.Load(reportFile);  //载入报表文件
        this.designerControl1.Report = dReport; //这里不一样的是把Report赋给控件的属性            
        DataSet ds1 = new DataSet();
        ds1 = getDataHz();
        dReport.RegisterData(ds1, "单据汇总");

        DataSet ds2 = new DataSet();
        ds2 = getDataMx();
        dReport.RegisterData(ds2, "单据明细");

        dReport.Prepare();   //准备
        dReport.Design();  //显示
    } 

    private DataSet getDataHz()
    {
        String connStr = ReturnDataSet.connectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        String sqlStr = ReturnDataSet.HzSql;
        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 DataSet getDataMx()
    {
        String connStr = ReturnDataSet.connectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        String sqlStr = ReturnDataSet.MxSql;
        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;
    }
}

}
创建打印预览From
报表生成器成功案例:C#调用FastReport控件示例

C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using System.Data.SqlClient;

namespace PrintTest001
{
public partial class FrmPrintPreview : Form
{
public FrmPrintPreview()
{
InitializeComponent();
}

    private void FrmPrintPreview_Load(object sender, EventArgs e)
    {
        Report dReport = new Report();   //实例化一个Report报表
        String reportFile = "Report/Report01.frx";
        dReport.Load(reportFile);  //载入报表文件
        dReport.Preview = previewControl1; //设置报表的Preview控件(这里的previewControl1就是我们之前拖进去的那个)    
        DataSet ds1 = new DataSet();
        ds1 = getDataHz();
        dReport.RegisterData(ds1, "单据汇总");

        DataSet ds2 = new DataSet();
        ds2 = getDataMx();
        dReport.RegisterData(ds2, "单据明细");

        dReport.Prepare();   //准备
        dReport.ShowPrepared();  //显示

    }

    private DataSet getDataHz()
    {
        String connStr = ReturnDataSet.connectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        String sqlStr = ReturnDataSet.HzSql;
        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 DataSet getDataMx()
    {
        String connStr = ReturnDataSet.connectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        String sqlStr = ReturnDataSet.MxSql;
        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;
    }
}

}
示例:

报表生成器成功案例:C#调用FastReport控件示例
打印设置效果:

报表生成器成功案例:C#调用FastReport控件示例

打印预览效果:

报表生成器成功案例:C#调用FastReport控件示例
还想要更多吗?您可以点击阅读【FastReport 报表2020最新资源盘点】,查找需要的教程资源。让人兴奋的是FastReport .NET正在慧都网火热销售中!慧都17周年庆惠享超低折扣,低至3701元起!

猜你喜欢

转载自blog.51cto.com/14874181/2576963