.NET MVC报表的制作

1,新建一个webForm页面,在该页面拖入ScriptManager和ReportViewer

2,在网站下面添加一个文件夹,例子(Reports文件夹)

3,在Reports文件夹中,选择新建项,添加一个“数据集”。。。后缀名是xsd的(例子。order.xsd)

4,在order.xsd拖放2个datatable控件,也就是相当于数据库的表。只要把想要的数据字段写进去,前提字段名字要和数据库表的列名一样
  datatable的名字建议和数据库表的名字一致(例子:Order   OrderDetail),最后还需要修改添加字段的数据类型,在属性处选择

5,在Reports文件夹中继续添加新建项,选择报表(Report1.rdlc)

7,在Report1.rdlc的工具箱中拖放控件,一般使用表和文本框

8,配置好数据集之后再webform窗体load里写代码
    using Microsoft.Reporting.WebForms;

    if (!IsPostBack) {
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/.rdlc的路径");
            ReportDataSource ds = new ReportDataSource();
            ds.Name = "";//dataset的名字
            ds.Value = "";//可以是一个集合,也可以是对象
            ReportViewer1.LocalReport.DataSources.Add(ds);
            ReportViewer1.LocalReport.Refresh();
        }


9,第八个是webForm的方式,那么要用MVC做报表的话,第八种方式就要改成这样
在控制器里面写一个action
public ActionResult Report(int id) {
            LocalReport localReport = new LocalReport();//new 一个报表对象
            localReport.ReportPath = Server.MapPath("~/Content/Reports/OrderReport.rdlc");//永远是报表
            Order order = orderBiz.FetchByKey(id);
            ReportDataSource rds1 = new ReportDataSource("Order", new List<Order> { order });
            localReport.DataSources.Add(rds1);
            ReportDataSource rds2 = new ReportDataSource("OrderDetail", order.Details);
            localReport.DataSources.Add(rds2);
        //下面是系统默认设置 直接复制就好
            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            //The DeviceInfo settings should be changed based on the reportType
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
            string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>8.5in</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;

            //Render the report
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
            return File(renderedBytes, mimeType);
        }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值