轉(zhuǎn)帖|實(shí)施案例|編輯:楊鵬連|2021-01-04 10:31:41.657|閱讀 381 次
概述:本文介紹了使用功能強(qiáng)大的插件FastReport.Net打印報(bào)表實(shí)例。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
FastReport .Net是適用于Windows Forms,ASP.NET,MVC和.NET Core的全功能報(bào)表解決方案。它可以在Microsoft Visual Studio 2005-2019中使用。支持.Net Framework 2.0-4.x,.NET Core 3.0及以上版本。
在FastReport .NET 2021.1的新版本中,我們實(shí)現(xiàn)了對(duì).NET 5的支持。添加了新條形碼-Deutsce Post Leitcode。將RTF轉(zhuǎn)換為報(bào)告對(duì)象的算法已得到顯著改進(jìn)。并且還添加了用于轉(zhuǎn)換數(shù)字的新功能。歡迎下載體驗(yàn)。(點(diǎn)擊下方按鈕下載)
立即點(diǎn)擊下載FastReport.NET v2021.1最新版
Fastreport.NET在線購買價(jià)更低,專享85折起!趕緊加入購物清單吧!
我第一次使用FastReport插件做的功能是打印一個(gè)十分復(fù)雜的excel表格,有幾百個(gè)字段都需要綁定數(shù)據(jù),至少需要4個(gè)數(shù)據(jù)源,而且用到橫向、豎向合并單元格。
我不是直接連接數(shù)據(jù)庫,而是使用RegisterData的方式自己注冊(cè)DataSet對(duì)象,所有的表Table都是代碼生成,填充到DataSet中,然后注冊(cè)到控件中。
最開始嘗試使用這個(gè)插件做一個(gè)簡單的功能使用的就是下面的例子,不過花了一整天,還請(qǐng)假了4個(gè)人都沒有搞出來,晚上拿著別人的模板直接修改,然后測(cè)試,就通過了。
之前測(cè)試,一直都提示未連接到數(shù)據(jù)源,原因是我直接使用記事本來向模板添加數(shù)據(jù)源導(dǎo)致的,平時(shí)別人使用的數(shù)據(jù)源都是自己寫代碼,調(diào)用Design的方式得到的,而且只能是winform程序,webform程序不行,因?yàn)橐獑?dòng)com組件。
下面是我總結(jié)的特別簡單的一個(gè)方式,使用RegisterData的方式自己注冊(cè)DataSet對(duì)象。
1.選擇【File】-》【New】 新建FastReport模板,選擇下圖的1。
2.選擇【View】-》【Data】,顯示如下,導(dǎo)出Dictionary,保存。
3.編輯導(dǎo)出的.frd文件,編輯完后保存,再導(dǎo)入。
如下:<?xml version="1.0" encoding="utf-8"?> <Dictionary> <TableDataSource Name="Table1" ReferenceName="Data.Table1" DataType="System.Int32" Enabled="true"> <Column Name="姓名" DataType="System.String" PropName="Column"/> <Column Name="密碼" DataType="System.String" PropName="Column"/> </TableDataSource> </Dictionary>4.編輯模板,添加一個(gè)Table控件。
第一行直接雙擊輸入文本;第二行直接將右邊的數(shù)據(jù)源托到單元格中;
設(shè)置邊框、字體。
5.添加事件后臺(tái)方法
選中Table1,在事件ManualBuild的后面雙擊,添加下面的代碼:// 控件Table1的構(gòu)建事件 private void Table1_ManualBuild(object sender, EventArgs e) { DataSourceBase data1 = Report.GetDataSource("Table1"); // 獲取DataSet中表名為Table1的數(shù)據(jù)源 data1.Init(); // 初始化 Table1.PrintRow(0); // 控件Table1打印第0行 Table1.PrintColumns(); // 每打印一行,都要調(diào)用 PrintColumn或PrintColumns while(data1.HasMoreRows) // 打印重復(fù)循環(huán)的行 { Table1.PrintRow(1); Table1.PrintColumns(); data1.Next(); // 讀取下一行 } }說明:
6.添加c#代碼
新建一個(gè)測(cè)試頁面test.aspx,將一個(gè)FastReport控件拖放到頁面上(只有按照過FastReport.net,且引用了FastReport.dll,F(xiàn)astReport.Bars.dll,F(xiàn)astReport.Web.dll之后才可以)
添加后頁面如下:
<body> <form id="form1" runat="server"> <cc1:WebReport ID="webReport" runat="server" OnStartReport="WebReport_StartReport" /> </form> </body>后臺(tái)方法:
protected void WebReport_StartReport(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable table1 = new DataTable(); table1.TableName = "Table1"; // 一定要設(shè)置表名稱 ds.Tables.Add(table1); // 添加表中的列 table1.Columns.Add("姓名", typeof(string)); table1.Columns.Add("密碼", typeof(string)); // 任意添加一些數(shù)據(jù) for (int i = 0, maxI = 10; i < maxI; i++) { DataRow row = table1.NewRow(); row["姓名"] = "我是" + i.ToString(); row["密碼"] = i.ToString(); table1.Rows.Add(row); } Report FReport = (sender as WebReport).Report; string sPath = GetReportsPath("test.frx") ; FReport.Load(sPath); // 將DataSet對(duì)象注冊(cè)到FastReport控件中 FReport.RegisterData(ds); } /// <summary> /// 獲取fastreport模板的路徑 /// </summary> /// <param name="sReportName">模板名稱</param> /// <returns>返回模板路徑</returns> public string GetReportsPath(string sReportName) { return FastReport.Utils.Config.ApplicationFolder + "Reports\\" + sReportName; }7. 測(cè)試效果
還想要更多嗎?您可以點(diǎn)擊閱讀【FastReport 報(bào)表2020最新資源盤點(diǎn)】,查找需要的教程資源。讓人興奮的是FastReport .NET正在慧都網(wǎng)火熱銷售中!慧都17周年慶惠享超低折扣,低至3701元起!>>查看價(jià)格詳情
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: