在 Aspnet Core 中将 RDLC 呈现为 PDF 或 Excel

Render RDLC as PDF or Excel in Aspnet Core

我听说在 aspnet core 2 中渲染 RDLC 是不可能的(或者不容易)。

是否可以在 Aspnet Core 3.0 或 3.1 中将 RDLC 呈现为 PDF 或 Excel?

PS: 但我在 Aspnet MVC 5 中有一个针对 .Net Framework 的工作代码。

找到了解决方案,我post这是为了使用以下软件包

<PackageReference Include="AspNetCore.Reporting" Version="2.1.0" /> 
<PackageReference Include="System.CodeDom" Version="4.7.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.1" />

但是,为了让报表设计器工作,我必须创建一个 .net 标准库项目,我在其中添加了 .rdcl 文件以用于设计目的。

之后我将最终文件移动到 Reports 目录(不在 wwwroot 内)并且我可以使用以下代码生成 PDF 文件(在 asp.net core 3.1 )

    string rdlcFilePath = Path.Combine(hostEnvironment.ContentRootPath, 
          "Reports", "UserDetails.rdlc");
        // return Ok(rdlcFilePath);
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        Encoding.GetEncoding("windows-1252");
        LocalReport report = new LocalReport(rdlcFilePath);

        List<UserDto> userList = getUserList;
        report.AddDataSource("dsUsers", userList);
        var result = report.Execute(GetRenderType("pdf"), 1, parameters);
        return File(result.MainStream, 
               System.Net.Mime.MediaTypeNames.Application.Octet,
               "users.pdf");

现在您可以将 href 添加到 a 元素以下载文件,或者如果您想要在 angular 服务中,您可以将其作为 blob 接收。

谢谢!