MVC使用RDL報表


MVC使用RDL報表

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

 

我們只能通過一些方式來使用rdl報表。

 

Razor視圖不支持asp.net服務器控件,但是aspx可以,所以用戶其實可以通過aspx視圖模版來顯示rdl報表或者水晶報表。

 

我是有強迫症的人,我不喜歡在眾多razor視圖中,讓aspx視圖鶴立雞群,所以這節主要是演示rdl在MVC中其中一種用法。

 

報表都有相似性  數據源-數據集-圖表-表組成

 

在MVC項目中新建一個數據源,這個數據源最后將由數據表、TableAdapter、查詢、關系組成,新建后可以點擊右鍵查看。

 

這里我們用到TableAdapter來演示,首先新建一張表

 

  SysSample

 

並錄入一些測試數據

 

  Test Data

 

一、創建數據源

 

 

二、選擇您的數據鏈接,如果你有鏈接數據庫的直接選擇即可

 

 

三、新建一個鏈接,最后它會在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可以直接在網頁預覽,如果是其他格式將獲得保存對話框彈出

 

 

右鍵選擇打印可以接本地打印機

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM