mvc3 应用程序中的 RDLC 报告

zia*_*lah 4 c# asp.net asp.net-mvc-3

需要在我的 MVC3 应用程序中生成报告,我如何在 mvc3 中使用 RDLC。请任何人都可以给我一个示例性的解释和指导方针,以便在我的 MVC3 应用程序中创建 RDLC 报告。

谢谢

Nat*_*her 5

我最近在 MVC3 应用程序中使用 RDLC 报告将结果导出到 Excel 电子表格中。

    private class ExcelReport 
    {
        private string encoding;
        private string[] streams;
        private Warning[] warnings; 
        private string fileNameExtension;
        private string mimeType; 
        public ExcelReport()
        {
            this.ReportDataSources = new List<ReportDataSource>();
        }
        public string ExportFileName { get; set; }
        public string MimeType
        {
            get
            {
                return mimeType;
            }
            private set
            {
                mimeType = value;
            }
        }
        public string Encoding
        {
            get
            {
                return encoding;
            }
            private set
            {
                encoding = value;
            }
        }
        public string FileNameExtension
        {
            get
            {
                return fileNameExtension;
            }
            private set
            {
                fileNameExtension = value;
            }
        }
        public string[] Streams
        {
            get
            {
                return streams;
            }
            private set
            {
                streams = value;
            }
        }
        public Warning[] Warnings
        {
            get
            {
                return warnings;
            }
            private set
            {
                warnings = value;
            }
        }
        public string ReportPath { get; set; }

        public IList<ReportDataSource> ReportDataSources { get; set; }
        public byte[] GetReport() 
        {

            LocalReport localReport = new LocalReport();
            localReport.ReportPath = this.ReportPath;

            foreach (var source in this.ReportDataSources)
            {
                localReport.DataSources.Add(source);
            }

            string reportType = "Excel";

            //The DeviceInfo settings should be changed based on the reportType             
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx             
            string deviceInfo =
                "<DeviceInfo>" +
                "  <OutputFormat>Excel</OutputFormat>" +
                "  <PageWidth>21cm</PageWidth>" +
                "  <PageHeight>29cm</PageHeight>" +
                "  <MarginTop>1cm</MarginTop>" +
                "  <MarginLeft>2cm</MarginLeft>" +
                "  <MarginRight>2cm</MarginRight>" +
                "  <MarginBottom>1cm</MarginBottom>" +
                "</DeviceInfo>";


            //Render the report             
            return localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

控制器

    public ActionResult GetReport(string reportParameter1) 
    {
        /*Get data for your report that matches the dataset format in the report.*/
        IEnumerable<ReportData> list = new IEnumerable<ReportData> /*Your Report Data*/
                                          {
                                             new ReportData{Id = 1, Code="ABC"},
                                             new ReportData{Id = 2, Code="DEF"}

                                          };
        var excelReport = new ExcelReport
        {
            ExportFileName = "Your File Name",
            ReportPath = Server.MapPath("~/Content/Reports/YourReport.rdlc")

        };
        var ds = new ReportDataSource("Main", list); /* Main is the name of the dataset inside the report*/
        excelReport.ReportDataSources.Add(ds);

        var report = excelReport.GetReport();

        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.{1}", excelReport.ExportFileName, excelReport.FileNameExtension));
        Response.ContentType = "application/vnd.ms-excel";

        return File(report, excelReport.MimeType);
    }
Run Code Online (Sandbox Code Playgroud)

最终结果应该是您将报告导出为 Excel 文档。