ASP.Net Core 3.1 RDLC报表表达式编译及格式化报错求助
ASP.Net Core RDLC报表表达式编译及格式化错误解决方案
问题详情
1. 行可见性表达式编译错误
设置报表行可见性为「基于表达式显示或隐藏」,使用表达式IIf(Fields!StudentAmt.Value=0,True,False)时,抛出异常:
ReportProcessingException: An unexpected error occurred while compiling expressions. Native compiler return value: ‘[BC30390] 'AspNetCore.ReportingServices.RdlExpressions.ExpressionHostObjectModel.DataRegionExprHost(Of TMemberType, TCellType).m_memberTreeHostsRemotable' is not accessible in this context because it is 'Friend'
2. 日期格式化数组越界错误
使用Format(Fields!OEndDate.Value, "MMM-yyyy")格式化日期时,抛出:
IndexOutOfRangeException: Index was outside the bounds of the array.
使用AspNetCore.Reporting库实现本地报表,Controller核心代码如下:
public IActionResult Report() { string mimtype = ""; int extension = 1; Dictionary<string, string> parameters = new Dictionary<string, string> { { "rpl", "Student Name" } }; LocalReport localReport = new LocalReport(mypath); localReport.AddDataSource("DataSet1", mydatasource); var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimtype); return File(result.MainStream, "application/pdf"); }
解决方案
针对行可见性表达式错误
- 降低库版本:将
AspNetCore.Reporting降级到3.1.1或2.1.0版本,这些版本对RDLC表达式编译的兼容性更稳定 - 简化表达式写法:直接使用布尔表达式替代
IIf函数,将表达式改为Fields!StudentAmt.Value=0,RDLC会直接识别该布尔值作为显示/隐藏的判断依据
针对日期格式化错误
Format函数在该库环境中存在bug,推荐以下替代方案:
- 使用
FormatDateTime函数:FormatDateTime(Fields!OEndDate.Value, DateFormat.ShortMonthYear) - 使用原生字符串格式化:
Fields!OEndDate.Value.ToString("MMM-yyyy"),若字段可能为空,可添加空值判断:IIf(IsNothing(Fields!OEndDate.Value), "", Fields!OEndDate.Value.ToString("MMM-yyyy"))
内容的提问来源于stack exchange,提问作者dausa
相关产品推荐
相关产品推荐

