報表生成器成功案例: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元起!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章