.NET Core第33天_AspNetCore.Reporting的使用再次擁抱RDLC做LocalReport報表列印

 
當你新增好.net core (model-view-controller)專案之後
請額外新增一個Windows Form的專案(for之後RDLC報表設計)

接著針對.net core MVC專案去Nuget下載這個套件
AspNetCore.Reporting







winform我們把RDLC部分設計好後將RDLC檔案drag到.net core mvc專案的wwwroot下面
額外創建一個Report目錄

以下是我們設計好的RDLC檔案


若對於RDLC使用沒有概念
可以參考之前寫過的一系列RDLC報表的文章







接著到.Net Core專案下
Program.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace NetCoreRDLC
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseWebRoot("wwwroot");
                    webBuilder.UseStartup<Startup>();
                });
    }
}


再控制器中額外增加ReportController.cs

並新增Print的Action程式
將webHostEnvironment透過建構子注入進來

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using AspNetCore.Reporting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCoreRDLC.Controllers
{
    public class ReportController : Controller
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        public ReportController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Print()
        {
            string mim_type = "";
            int extension = 1;
            var path = $"{this.webHostEnvironment.WebRootPath}\\Report\\Report1.rdlc";
            Dictionary<string,string> parms = new Dictionary<string, string>();
            parms.Add("rp1", "Welcome .net core rdlc");
            LocalReport localReport = new LocalReport(path);
            var result = localReport.Execute(RenderType.Pdf, extension, parms, mim_type);
            return File(result.MainStream, "application/pdf");
        }
    }
}

這樣子就可以實踐報表pdf的頁面列印之前的呈現




那示範完參數化RDLC結果的列印後
更常見的就是從DB撈取的最終結果集要弄成表格形式報表列印出來


















留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header