一、基本使用
1、准備工程和引入控件
1、下載、安裝FastReport
這一步很簡單,大家在其中文網站上下載最新版的demo版就可以了,直接安裝就可以
替換破解文件:
Replace C:\Windows\Microsoft.NET\assembly\GAC_MSIL\FastReport\v4.0_2019.1.5.0__00000000000000000000000000\FastReport.dll with cracked one.
Assemblies from folders Framework X.0 is PublicKeyToken removed and strong name verification disabled.
安裝之后大家會發現,VS里面什么都沒有,不像有些插件直接會在ToolBox里顯示,這里需要我們自己引入
2、准備工程、引入控件
首先我們使用VS新建一個WinForm工程,這里我使用的是VisualStutio2015版本
接着我們先引入FastReport的核心dll依賴,這些文件的目錄在FastReport安裝目錄下,分別是FastReport.dll,FastReport.Editor.dll,FastReport.Bars.dll。
你可以使用Framework 4.0下的dll文件
接着我們需要3個窗體:MainForm,DesignForm,PreviewForm,其中MainForm為啟動頁面
現在我們需要在ToolsBox中引入我們需要的FastReport控件,首先我們在ToolsBox中新建一個Item,命名為FastReport
然后右鍵剛剛新建的選項卡->選擇項,打開選擇控件的對話框
然后我們點擊左下角的瀏覽,選擇剛剛的FastReport.dll,然后確定,之后再確定,就會成功導入以下新的控件
3、啟動頁設計
MainForm很簡單,我們就放兩個按鈕,一個設計,一個瀏覽,分別打開兩個窗口
事件
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報表
使用安裝時我們的設計工具設計一張最簡單的報表
設計的報表,只有一個文字框
將這份報表保存到工程文件/bin/Debug/Report下
2、引入Preview控件
我們在PreviewForm中,將PreviewControl控件拖入窗體,將窗體拉大一點,然后將控件的Dock設為Fill
然后我們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一下,載入了報表文件的樣子
這里我們已經可以預覽我們的報表了 但是在我們的需求中,用戶還需要自定義報表的內容和格式呢,我們下一步就在實現報表設計器
3、引入Design控件
我們像Preview那樣把Design控件拖進DesignForm,然后Dock設為Fill
然后我們來寫怎么樣吧設計器綁定Report文件,雙擊新建Load函數,引入FastReport,新建一個private變量
using FastReport; using System; using System.Windows.Forms; namespace ReportDemo { public partial class DesignForm : Form { private Report dReport; public DesignForm() { InitializeComponent(); } private void DesignForm_Load(object sender, EventArgs e) { dReport = new Report(); string reportFile = "Report/report.frx"; dReport.Load(reportFile); this.designerControl1.Report = dReport; dReport.Prepare(); dReport.Design(); } } }
我們F5一下
成功!
3、綁定數據
1、數據庫准備
我們使用VisualStudio自帶的mdf文件數據庫,首先我們在工程中創建一個文件夾APP_DATA,在此文件夾中創建一個mdf文件
然后我們可以在服務器資源管理器中看到我們的數據庫
然后我們右鍵表新建一個表
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 )
然后在設計器左上角點擊更新按鈕,在彈出的窗口中點擊更新數據庫
更狀態全部打鈎之后,表就創建好了,我們刷新服務器資源管理器,然后打開表數據,添加一些數據進去
ok我們現在在服務器資源管理器里面選擇mdf文件,在屬性列表里,找到連接字符串,拷貝一份出來,等會需要用的到
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()
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; }
然后我們在Form_Load方法里綁定數據集
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一下,在設計窗口下,在[數據]->[選擇數據源]中,就能看到我們綁定的數據了
我們設計一個表格,把我們的數據放進去
我們可以預覽一下,然后保存
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(); //顯示 }
我們測試一下
二、用戶自定義報表,可保存到服務器和打開。
摘自官方Demo:
調用設計器界面
首頁代碼
public partial class Form1 : Form { private DataSet FReportsDs; private DataTable ReportsTable { get { return FReportsDs.Tables[0]; } } public Form1() { InitializeComponent(); } private void InitializeDatabase() { FReportsDs = new DataSet(); FReportsDs.ReadXml(Config.ApplicationFolder + @"..\..\database.xml"); } private void FinalizeDatabase() { FReportsDs.WriteXml(Config.ApplicationFolder + @"..\..\database.xml", XmlWriteMode.WriteSchema); } private void WireupDesignerEvents() { Config.DesignerSettings.CustomOpenDialog += new OpenSaveDialogEventHandler(DesignerSettings_CustomOpenDialog); Config.DesignerSettings.CustomOpenReport += new OpenSaveReportEventHandler(DesignerSettings_CustomOpenReport); Config.DesignerSettings.CustomSaveDialog += new OpenSaveDialogEventHandler(DesignerSettings_CustomSaveDialog); Config.DesignerSettings.CustomSaveReport += new OpenSaveReportEventHandler(DesignerSettings_CustomSaveReport); } private void DesignReport() { using (Report report = new Report()) { report.LoadBaseReport += new CustomLoadEventHandler(report_LoadBaseReport); report.Design(); } } // this event is fired when loading a base part of an inherited report. private void report_LoadBaseReport(object sender, CustomLoadEventArgs e) { OpenReport(e.Report, e.FileName); } // this event is fired when the user press the "Open file" button private void DesignerSettings_CustomOpenDialog(object sender, OpenSaveDialogEventArgs e) { using (OpenDialogForm form = new OpenDialogForm()) { // pass the reports table to display a list of reports form.ReportsTable = ReportsTable; // show dialog e.Cancel = form.ShowDialog() != DialogResult.OK; // return the selected report in the e.FileName e.FileName = form.ReportName; } } // this event is fired when report needs to be loaded private void DesignerSettings_CustomOpenReport(object sender, OpenSaveReportEventArgs e) { OpenReport(e.Report, e.FileName); } // this event is fired when the user press the "Save file" button to save untitled report, // or "Save file as" button private void DesignerSettings_CustomSaveDialog(object sender, OpenSaveDialogEventArgs e) { using (SaveDialogForm form = new SaveDialogForm()) { // show dialog e.Cancel = form.ShowDialog() != DialogResult.OK; // return the report name in the e.FileName e.FileName = form.ReportName; } } // this event is fired when report needs to be saved private void DesignerSettings_CustomSaveReport(object sender, OpenSaveReportEventArgs e) { SaveReport(e.Report, e.FileName); } private void OpenReport(Report report, string reportName) { // find the datarow with specified ReportName foreach (DataRow row in ReportsTable.Rows) { if ((string)row["ReportName"] == reportName) { // load the report from a stream contained in the "ReportStream" datacolumn byte[] reportBytes = (byte[])row["ReportStream"]; using (MemoryStream stream = new MemoryStream(reportBytes)) { report.Load(stream); } return; } } } private void SaveReport(Report report, string reportName) { // find the datarow with specified ReportName DataRow reportRow = null; foreach (DataRow row in ReportsTable.Rows) { if ((string)row["ReportName"] == reportName) { reportRow = row; break; } } // no existing row found, append new one if (reportRow == null) { reportRow = ReportsTable.NewRow(); ReportsTable.Rows.Add(reportRow); } // save the report to a stream, then put byte[] array to the datarow using (MemoryStream stream = new MemoryStream()) { report.Save(stream); reportRow["ReportName"] = reportName; reportRow["ReportStream"] = stream.ToArray(); } } private void Form1_Load(object sender, EventArgs e) { InitializeDatabase(); WireupDesignerEvents(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { FinalizeDatabase(); } private void btnDesign_Click(object sender, EventArgs e) { DesignReport(); } }
打開對話框:
public partial class OpenDialogForm : Form { public DataTable ReportsTable { set { // fill the listbox with names of reports foreach (DataRow row in value.Rows) { lbxReports.Items.Add(row["ReportName"]); } } } public string ReportName { get { return (string)lbxReports.SelectedItem; } } public OpenDialogForm() { InitializeComponent(); } private void lbxReports_SelectedIndexChanged(object sender, EventArgs e) { btnOK.Enabled = !String.IsNullOrEmpty(ReportName); } }
保存對話框
public partial class SaveDialogForm : Form { public string ReportName { get { return tbReportName.Text; } } public SaveDialogForm() { InitializeComponent(); } private void tbReportName_TextChanged(object sender, EventArgs e) { btnOK.Enabled = !String.IsNullOrEmpty(ReportName); } }
參考:使用report.ReportResourceString在數據庫中保存FastReport.Net報表,
https://segmentfault.com/a/1190000010467696
鏈接:https://pan.baidu.com/s/1FlRVWg3yI_qlQL0OQM7pmQ
提取碼:yf2x