ReportDocument.ReportSource只读无法赋值的问题求助
解决CrystalReport导出PDF时ReportSource只读报错问题
问题原因
你遇到的报错Property or Indexer 'ReportDocument.reportSource'cannot be assigned to -- it is read only,是因为VacuumByDayCrystalReport1的类型为ReportDocument,而该类的ReportSource属性是只读的,无法直接赋值。
解决方法
方法一:仅导出PDF(无需界面显示报表)
如果你的需求只是导出PDF,不需要在界面展示报表,直接移除报错的两行代码即可——导出操作仅依赖你已创建并加载好的cryRpt实例。修改后的代码如下:
ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(@"C:\temp\ExportVacumeren\ExportVacumeren.rpt"); cryRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"C:\temp\ExportVacumeren\" + Global.Day + " - " + Global.JaWe + " Vacumeren.pdf"); MessageBox.Show("Exported Vacumeren Successful");
方法二:需显示报表后再导出
如果需要先在界面显示报表再执行导出,需确认VacuumByDayCrystalReport1是CrystalReportViewer控件(而非ReportDocument实例):
- 若它是
CrystalReportViewer控件,直接赋值ReportSource即可(该控件的ReportSource属性是可写的); - 若它确实是
ReportDocument实例,直接使用它加载报表并执行导出,无需额外创建cryRpt:
VacuumByDayCrystalReport1.Load(@"C:\temp\ExportVacumeren\ExportVacumeren.rpt"); VacuumByDayCrystalReport1.Refresh(); VacuumByDayCrystalReport1.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"C:\temp\ExportVacumeren\" + Global.Day + " - " + Global.JaWe + " Vacumeren.pdf"); MessageBox.Show("Exported Vacumeren Successful");
内容的提问来源于stack exchange,提问作者Ronald Roenhorst
相关产品推荐
相关产品推荐

