轉帖|實施案例|編輯:楊鵬連|2021-06-28 17:13:12.730|閱讀 434 次
概述:本文介紹了報表生成器C#調用FastReport控件成功示例研究。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
	隨著現代技術的高速發展,相關產業所衍生出來的數據集是越來越龐大。那么我們如何能夠簡單、方便、快捷的展現自己輸入數據?并且能夠以我們想要的方式展現出來?報表——這一產物便應運而生,現在市面上流行的報表工具類產品也是層出不窮。
 
我們為什么使用第三方報報表開發工具,而不使用Excel呢?
Excel是一個電子表格程序,而不是一個數據庫程序。Excel數據處理容量和速度有限制,數據可視化程度不高,都是以表格為主,雖然也能插入一些圖表,但是靈活度和美觀度不夠,設置起來也相當麻煩,并且數據獲取麻煩。
第三方報表工具是數據庫存儲,數據庫程序通常可以存放的數據量是相當大的,可以處理非常復雜的數據結構關系。報表數據交互也快捷方便,速度也非常快,可視化交互渲染。
創建個WinForm項目
 
 
引用dll文件
	
 
	
引用dll文件創建FastReport控件工具
	
 
創建打印設置From
	
 
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#代碼:
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;
        }
    }
}
示例:
 
 
打印設置效果:
	 
 
	 
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都網