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

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

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

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

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

 将程序包源切换为默认的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<Product> FProducts;

        public string Name
        {
            get { return FName; }
        }

        public string Description
        {
            get { return FDescription; }
        }

        public List<Product> Products
        {
            get { return FProducts; }
        }

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

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<Category> 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 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
<button @onclick="GotoReport">测试报表</button>
<button onclick="window.open('/api/FastReport/Test')">测试报表2</button>
@code {



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

}

四、效果

五、B站视频

WTMBlazor采用webapiapi文件返回方式接入fastreport_哔哩哔哩_bilibiliicon-default.png?t=LA92https://www.bilibili.com/video/BV1kf4y1M7QM/

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值