ASP.NET MVC5+EF6+EasyUI 後臺管理系統(31)-MVC使用RDL報表

系列目錄html

此次咱們來演示MVC3怎麼顯示RDL報表,坑爹的微軟把MVC升級到5都木有良好的支持報表,讓MVC在某些領域趨於短板前端

咱們只能經過一些方式來使用rdl報表。web

Razor視圖不支持asp.net服務器控件,可是aspx能夠,因此用戶其實能夠經過aspx視圖模版來顯示rdl報表或者水晶報表。sql

我是有強迫症的人,我不喜歡在衆多razor視圖中,讓aspx視圖鶴立雞羣,因此這節主要是演示rdl在MVC中其中一種用法。數據庫

報表都有類似性  數據源-數據集-圖表-表組成瀏覽器

在MVC項目中新建一個數據源,這個數據源最後將由數據表、TableAdapter、查詢、關係組成,新建後能夠點擊右鍵查看。服務器

這裏咱們用到TableAdapter來演示,首先新建一張表asp.net

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

並錄入一些測試數據ide

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

1、建立數據源測試

2、選擇您的數據連接,若是你有連接數據庫的直接選擇便可

3、新建一個連接,最後它會在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" />

4、配置嚮導

有多種方式供用戶選擇。我這裏方便的使用了sql語句

輸入select * from SysSample一條查詢語句,接下來全勾上,每一個勾都寫得很清楚

 

數據集已經建立完畢

5、建立RDL

新建一個文件夾。專門來存放rdl -----> Reports

在Reports下建立SysSampleReport.rdlc文件

6、爲報表建立數據集,數據源選擇咱們剛剛建立的AppDBDataSet數據源

7、隨便添加一個圖標經常使用的餅圖和列表(老實說過若是不懂先右鍵)

 

上面說的都是建立報表的基礎。咱們早在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能夠直接在網頁預覽,若是是其餘格式將得到保存對話框彈出

右鍵選擇打印能夠接本地打印機

相關文章
相關標籤/搜索