You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决使用AspNetCore.Reporting库打印多份Rdlc报表的问题

AspNetCore.Reporting 库默认开启了 RDLC 报表定义缓存,首次加载报表后会将解析后的报表结构存在内存中,后续即使实例化新的 LocalReport 对象传入不同路径,也会优先读取缓存中的旧报表结构,不会加载新的 RDLC 文件,最终导致多报表生成异常。

修复步骤

方案一:禁用当前报表实例缓存(推荐,改动最小)

只需在实例化 LocalReport 后新增一行代码关闭缓存即可,同时建议使用 Path.Combine 拼接路径,避免转义符、跨平台兼容问题,修改后的完整代码如下:

string path = "";
string mimtype = "";
int extension = 1;
Dictionary<string, string> parameters = new Dictionary<string, string>();

if (rpt == 1)
{
    path = Path.Combine(_env.WebRootPath, "Reports", "Report1.rdlc");
    parameters = g.GetAll();
}
if (rpt == 2)
{
    path = Path.Combine(_env.WebRootPath, "Reports", "Report2.rdlc");
    parameters = g.GetAll2();
}
if (rpt == 3)
{
    path = Path.Combine(_env.WebRootPath, "Reports", "Report3.rdlc");
    parameters = g.etatFinAutres(_context.Balance.ToList());
}

LocalReport localReport = new LocalReport(path);
// 新增这行代码禁用缓存,每次强制重新加载RDLC文件
localReport.EnableLocalReportCache = false;
var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimtype);
return File(result.MainStream, "application/pdf");

方案二:单次渲染后清理缓存

如果需要保留缓存提升单报表重复生成的性能,可以在每次报表渲染完成后主动清理缓存:

var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimtype);
// 渲染完成后清理当前实例缓存
localReport.ClearCache();
return File(result.MainStream, "application/pdf");

内容的提问来源于stack exchange,提问作者Carlos Kanika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 05:27:02