WTM(ASP.NET 5)接入fastreport简单通用方案(采用api方式用流文件返回)

       目前使用WTM小伙伴目前前端的UI选择有layui、vue和blazor三种方案。为了方便起见,采用api的方式,对fastreport生成的pdf报表统一流文件返回。

一、导入设置本地nuget源,并导入fastreport类库。

在vs菜单栏中选择工具→Nuget报管理器→程序包管理器设置→程序包源中添加fastreport的本地包源,将源地址指向你的fastreport nuget路径,路径中最好不要有中文。 

WTM(ASP.NET 5)接入fastreport简单通用方案(采用api方式用流文件返回)_第1张图片

在nuget中将程序包源切换本地fastreport源,搜索fastreport.Core,选择Fastreport.Core并安装。

WTM(ASP.NET 5)接入fastreport简单通用方案(采用api方式用流文件返回)_第2张图片

 将程序包源切换为默认的nuget.org,搜索FastReport.Compat,并安装。注意:这里有个坑,如果是WTM项目,则需要先安装Microsoft.CodeAnalysis.Common类库,版本在3.8.0以上。默认的模板项目则不需要。

二、导入做好的fastreport模板,.frx文件

在web项目的wwwroot文件下,新建report目录。将做好的.frx文件放复制进去。github下载.frx示例文件。

三、代码编写

示例代码:

先新建2个测试用的实体类:

public class Category
    {
        private string FName;
        private string FDescription;
        private List FProducts;

        public string Name
        {
            get { return FName; }
        }

        public string Description
        {
            get { return FDescription; }
        }

        public List Products
        {
            get { return FProducts; }
        }

        public Category(string name, string description)
        {
            FName = name;
            FDescription = description;
            FProducts = new List();
        }
    }

public class Product
    {
        private string FName;
        private decimal FUnitPrice;

        public string Name
        {
            get { return FName; }
        }

        public decimal UnitPrice
        {
            get { return FUnitPrice; }
        }

        public Product(string name, decimal unitPrice)
        {
            FName = name;
            FUnitPrice = unitPrice;
        }
    }

[Area("TEST")]
    [AuthorizeJwtWithCookie]
    [ActionDescription("报表管理")]
    [ApiController]
    [Route("api/FastReport")]
    [Public]
    public class FastReportController : Controller
    {
        private readonly IWebHostEnvironment _hostingEnvironment;
        public FastReportController(IWebHostEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        private static List businessObjects;

        [HttpGet("Test")]
        public IActionResult Test()
        {
            string webRootPath = _hostingEnvironment.WebRootPath ;
            CreateBusinessObject();
            Report report = new Report();
            report.Load(webRootPath + "\\Report\\report.frx");
            report.RegisterData(businessObjects, "Categories");
            report.Prepare();
            PDFExport export = new PDFExport();
            MemoryStream stream = new MemoryStream();
            report.Export(export, stream);
            var t = stream.GetBuffer();
            stream.Close();
            report.Dispose();

            Response.Headers.Add("Cache-Control", "max-age=31536000, must-revalidate");
            Response.Headers.Add("accept-ranges", "bytes");
            Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");

            return File(t, "application/pdf");




        }

        private static void CreateBusinessObject()
        {
            businessObjects = new List();

            Category category = new Category("Beverages", "Soft drinks, coffees, teas, beers");
            category.Products.Add(new Product("Chai", 18m));
            category.Products.Add(new Product("Chang", 19m));
            category.Products.Add(new Product("Ipoh coffee", 46m));
            businessObjects.Add(category);

            category = new Category("Confections", "Desserts, candies, and sweet breads");
            category.Products.Add(new Product("Chocolade", 12.75m));
            category.Products.Add(new Product("Scottish Longbreads", 12.5m));
            category.Products.Add(new Product("Tarte au sucre", 49.3m));
            businessObjects.Add(category);

            category = new Category("Seafood", "Seaweed and fish");
            category.Products.Add(new Product("Boston Crab Meat", 18.4m));
            category.Products.Add(new Product("Red caviar", 15m));
            businessObjects.Add(category);
        }
    }

在PC的前端,直接window.open(url)扔给浏览器就可以了。简单粗暴!

例如blazor

@page "/Report/TestPeport"
@inherits BasePage
@attribute [ActionDescription("fastreport测试", "BlazorFastReportDemo.Areas.FastReport.Controllers,FastReport")]
@inject IJSRuntime JS


@code {



    private async Task GotoReport()
    {
        await JS.InvokeVoidAsync("$.test", "/api/FastReport/Test");
    }

}

四、效果

WTM(ASP.NET 5)接入fastreport简单通用方案(采用api方式用流文件返回)_第3张图片

WTM(ASP.NET 5)接入fastreport简单通用方案(采用api方式用流文件返回)_第4张图片

你可能感兴趣的:(WTM随写,asp.net,vue.js,前端)