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可以直接在网页预览,如果是其他格式将获得保存对话框弹出

 

 

右键选择打印可以接本地打印机

 

原文地址:https://www.cnblogs.com/Leo_wl/p/3594635.html