構建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的後台管理系統(31)-MVC使用RDL報表

來源:互聯網
上載者:User

標籤:des   style   blog   class   c   code   

原文:構建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的後台管理系統(31)-MVC使用RDL報表

這次我們來示範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
SysSample

並錄入一些測試資料

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‘)
Test Data

一、建立資料來源

二、選擇您的資料連結,如果你有連結資料庫的直接選擇即可

三、建立一個連結,最後它會在web.config產生一個節點

<add name="AppDBConnectionString" connectionString="Data Source=.;Initial Catalog=AppDB;User ID=sa;[email protected]#;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這個方法

在Google瀏覽器輸出PDF可以直接在網頁預覽,如果是其他格式將獲得儲存對話方塊彈出

右鍵選擇列印可以接本機印表機

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.