這次我們來演示MVC3怎么顯示RDL報表,坑爹的微軟把MVC升級到5都木有良好的支持報表,讓MVC在某些領域趨於短板
我們只能通過一些方式來使用rdl報表。
Razor視圖不支持asp.net服務器控件,但是aspx可以,所以用戶其實可以通過aspx視圖模版來顯示rdl報表或者水晶報表。
我是有強迫症的人,我不喜歡在眾多razor視圖中,讓aspx視圖鶴立雞群,所以這節主要是演示rdl在MVC中其中一種用法。
報表都有相似性 數據源-數據集-圖表-表組成
在MVC項目中新建一個數據源,這個數據源最后將由數據表、TableAdapter、查詢、關系組成,新建后可以點擊右鍵查看。
這里我們用到TableAdapter來演示,首先新建一張表

CREATE TABLE [dbo].[SysSample]( [Id] [varchar](50) NOT NULL, [Name] [varchar](50) NULL, [Age] [int] NULL, [Bir] [datetime] NULL, [Photo] [varchar](50) NULL, [Note] [text] NULL, [CreateTime] [datetime] NULL, CONSTRAINT [PK__SysSampl__3214EC075AEE82B9] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
並錄入一些測試數據

INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('1','張三',18,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('11','張三',18,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('2','李四',21,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('22','李四',21,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('3','王五',33,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('33','王五',33,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('4','柳六',24,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('44','柳六',24,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('5','X七',65,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM') INSERT INTO [SysSample] ([Id],[Name],[Age],[Bir],[Photo],[Note],[CreateTime]) values ('55','X七',65,'10 1 1991 12:00AM',NULL,NULL,'01 1 2013 12:00AM')
一、創建數據源
二、選擇您的數據鏈接,如果你有鏈接數據庫的直接選擇即可
三、新建一個鏈接,最后它會在web.config生成一個節點
<add name="AppDBConnectionString" connectionString="Data Source=.;Initial Catalog=AppDB;User ID=sa;Password=zhaoyun123!@#;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
四、配置向導
有多種方式供用戶選擇。我這里方便的使用了sql語句
輸入select * from SysSample一條查詢語句,接下來全勾上,每個勾都寫得很清楚
數據集已經創建完畢
五、創建RDL
新建一個文件夾。專門來存放rdl -----> Reports
在Reports下創建SysSampleReport.rdlc文件
六、為報表創建數據集,數據源選擇我們剛剛創建的AppDBDataSet數據源
七、隨便添加一個圖標常用的餅圖和列表(老實說過如果不懂先右鍵)
上面說的都是創建報表的基礎。我們早在asp.net頁面已經熟悉了,回到Controller
添加以下方法(type = PDF,Excel,Word )
public ActionResult Reporting(string type = "PDF", string queryStr = "", int rows = 0, int page = 1) { //選擇了導出全部 if (rows == 0 && page == 0) { rows = 9999999; page = 1; } GridPager pager = new GridPager() { rows = rows, page = page, sort="Id", order="desc" }; List<SysSampleModel> ds = m_BLL.GetList(ref pager, queryStr); LocalReport localReport = new LocalReport(); localReport.ReportPath = Server.MapPath("~/Reports/SysSampleReport.rdlc"); ReportDataSource reportDataSource = new ReportDataSource("DataSet1", ds); localReport.DataSources.Add(reportDataSource); string reportType = type; string mimeType; string encoding; string fileNameExtension; string deviceInfo = "<DeviceInfo>" + "<OutPutFormat>" + type + "</OutPutFormat>" + "<PageWidth>11in</PageWidth>" + "<PageHeight>11in</PageHeight>" + "<MarginTop>0.5in</MarginTop>" + "<MarginLeft>1in</MarginLeft>" + "<MarginRight>1in</MarginRight>" + "<MarginBottom>0.5in</MarginBottom>" + "</DeviceInfo>"; Warning[] warnings; string[] streams; byte[] renderedBytes; renderedBytes = localReport.Render( reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings ); return File(renderedBytes, mimeType); }
所以呢。沒有傳說的那么神秘,靠輸出來制作報表
- List<SysSampleModel> ds把讀取到的列表賦予給ds
- localReport.ReportPath指定報表的路徑
- ReportDataSource reportDataSource = new ReportDataSource("DataSet1", ds);指定數據集 DataSet1
填充好數據集,最后的前端就是調用 Reporting這個方法
在谷歌瀏覽器輸出PDF可以直接在網頁預覽,如果是其他格式將獲得保存對話框彈出
右鍵選擇打印可以接本地打印機