Stimulsoft 2017导出Excel报表无设置弹窗且自定义配置不生效
问题修复方案
1. 导出设置弹窗不弹出的问题
检查StiReportViewer控件的ShowExportDialog属性配置,该属性默认值为true,如果被手动设置为false,就会跳过导出设置弹窗直接执行导出逻辑,改回true即可恢复弹窗。
如果你的需求就是不需要弹窗、走自定义导出逻辑,可忽略该配置,直接参考下面的自定义导出修复步骤。
2. 自定义导出配置不生效的问题
你现有代码存在三个核心问题导致配置不生效:
- 禁用默认导出响应的代码被注释,控件默认导出逻辑和你自定义的导出逻辑冲突,最终生效的是默认导出规则
- 导出格式硬编码为
StiExportFormat.Excel,和你判断分支里包含的Excel2007格式不匹配,选xlsx格式导出时配置不会生效 - 异常被完全吞掉,导出过程中出现错误也无法感知排查
修正后的代码
void StiReportViewer_ReportExport(object sender, StiExportDataEventArgs e) { try { StiExportFormat exportFormat = e.Settings.GetExportFormat(); if (exportFormat == StiExportFormat.Excel || exportFormat == StiExportFormat.Excel2007) { // 禁用控件默认导出响应,必须取消注释否则自定义配置不生效 this.StiReportViewer.ExportResponse = false; Stimulsoft.Report.Export.StiExcelExportSettings stiExcelExportSettings = new Stimulsoft.Report.Export.StiExcelExportSettings(); stiExcelExportSettings.ExportDataOnly = true; stiExcelExportSettings.UseOnePageHeaderAndFooter = true; stiExcelExportSettings.ExportPageBreaks = false; stiExcelExportSettings.ExportObjectFormatting = false; MemoryStream xlsMemoryStream = new MemoryStream(); // 改用动态获取的导出格式,不要硬编码 e.Report.ExportDocument(exportFormat, xlsMemoryStream, stiExcelExportSettings); Response.Clear(); // 区分格式设置对应的文件名和ContentType,避免文件打不开 string fileName = exportFormat == StiExportFormat.Excel ? "Report.xls" : "Report.xlsx"; string contentType = exportFormat == StiExportFormat.Excel ? "application/ms-excel" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", $"attachment; filename={fileName}"); Response.ContentType = contentType; Response.BinaryWrite(xlsMemoryStream.ToArray()); Response.End(); } } catch (Exception ex) { // 建议这里添加日志输出异常信息,方便后续排查问题 } }
补充注意事项
如果调整后配置仍不生效,可在调用ExportDocument前先执行e.Report.Render(),确保报表完全渲染后再执行导出。Stimulsoft 2017的部分早期小版本存在ExportObjectFormatting参数兼容问题,如果该参数始终不生效可尝试升级到2017.2.x以上的小版本。
内容的提问来源于stack exchange,提问作者user10997800
相关产品推荐
相关产品推荐

