成功案例02:C#调用FastReport控件示例

百度上搜索FastReport组件必须的dll文件,如下图:

创建个WinForm项目

成功案例02:C#调用FastReport控件示例_第1张图片

引用dll文件

成功案例02:C#调用FastReport控件示例_第2张图片

引用dll文件创建FastReport控件工具

成功案例02:C#调用FastReport控件示例_第3张图片

创建打印设置From

成功案例02:C#调用FastReport控件示例_第4张图片

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

成功案例02:C#调用FastReport控件示例_第5张图片

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

示例:

成功案例02:C#调用FastReport控件示例_第6张图片 

打印设置效果:

成功案例02:C#调用FastReport控件示例_第7张图片

打印预览效果:

成功案例02:C#调用FastReport控件示例_第8张图片

你可能感兴趣的:(C#)