ASP.NET-MVC-REPORT 的樣板CODE

  • 806
  • 0
  • 2014-02-06

摘要:ASP.NET-MVC-REPORT 的樣板CODE

這是一段神祕的CODE,關於轉出 PDF Excel Word Image 的東西,

由於作法太長,好懶得打阿~~

事前準備

1. 專案加入C:\Program Files (x86)\Microsoft Visual Studio 11.0\ReportViewer\Microsoft.ReportViewer.WebForms.dll

2.建立DATASET 

3.建立 報表檔 Report.rdlc,畫表格樣版

4.綁定  DATASET  IN  Report.rdlc 內的各項欄位

5.使用底下的神祕CODE

 

        //  id  in (PDF Excel Word Image)
        public ActionResult Report(string id)
        {
            Microsoft.Reporting.WebForms.LocalReport lr = new Microsoft.Reporting.WebForms.LocalReport();
            string path = System.IO.Path.Combine(Server.MapPath("~/Report"), "Report1.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<MvcApplication2.Models.A003> cm = new List<MvcApplication2.Models.A003>();
            using (MvcApplication2.Models.iDbEntitiesdb = new MvcApplication2.Models.iDbEntities())
            {
                cm = db.A003.Take(100).ToList();
            }
            Microsoft.Reporting.WebForms.ReportDataSource rd = 
                new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;
 
            string deviceInfo =
 
            "<DeviceInfo>" +
            //"  <OutputFormat>" + id + "</OutputFormat>" +
            //"  <PageWidth>8.5in</PageWidth>" +
            //"  <PageHeight>11in</PageHeight>" +
            //"  <MarginTop>0.5in</MarginTop>" +
            //"  <MarginLeft>1in</MarginLeft>" +
            //"  <MarginRight>1in</MarginRight>" +
            //"  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";
 
            Microsoft.Reporting.WebForms.Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
 
            renderedBytes = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
 
            return File(renderedBytes, mimeType);
   }
 
以上~  不負責任的發言