.NET MVC報表的製作

來源:互聯網
上載者:User

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);
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.