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

SSRS Report Execution Service导出xlsx时Excel365提示修复错误如何解决

解决方案

你的导出报错核心是代码里的MIME类型配置错误,加上响应流处理不规范导致,按以下步骤修改即可:

  • 首先修正ContentType错误,你的代码里多写了一层application/前缀,正确的xlsx MIME类型为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • 替换仅调用Response.ClearContent()的逻辑,改为先调用Response.Clear()清空所有响应内容和头信息,避免残留数据污染文件流
  • 执行完Response.BinaryWrite()和Response.Flush()之后追加Response.End(),终止响应输出,防止页面其他冗余内容被追加到Excel文件末尾
  • 可选补充:调用service.Render后可以判断warnings数组是否为空,排查SSRS导出时是否存在布局类警告,这类警告也可能导致Excel打开时报错

修改后的代码如下:

private void DownloadQuoteExport()
{
    string reportName = "reportName";
    string fileName = "filename";
    //create web services instance
    ReportExecutionService rs = getService();
    //render report 1st parameter
    ParameterValue param1 = new ParameterValue();
    param1.Name = "QuoteID";
    param1.Value = quoteId.ToString();
    try
    {
        executeReport(reportName, new ParameterValue[] { param1 }, "EXCELOPENXML", fileName, rs);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + "<br>" + ex.StackTrace.ToString());
    }
}

private void executeReport(String reportName, ParameterValue[] rptParams, String rptFormat, string strRptFileName, ReportExecutionService service)
{
    string encoding;
    string mimeType;
    string extension;
    Warning[] warnings = null;
    string[] streamIDs = null;
    string historyID = null;

    ExecutionInfo execInfo = new ExecutionInfo();
    ExecutionHeader execHeader = new ExecutionHeader();

    service.ExecutionHeaderValue = execHeader;
    execInfo = service.LoadReport(reportName, historyID);

    service.SetExecutionParameters(rptParams, "en-us");
    String SessionId = service.ExecutionHeaderValue.ExecutionID;
    byte[] result = service.Render(rptFormat, null, out extension, out encoding, out mimeType, out warnings, out streamIDs);

    // 改动1:清空所有响应内容和头
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();

    if (rptFormat == "EXCELOPENXML")
    {
        // 改动2:修正MIME类型
        Response.AppendHeader("content-disposition", "attachment; filename=" + strRptFileName + ".xlsx");
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

    Response.BinaryWrite(result);
    Response.Flush();
    // 改动3:终止响应,避免后续内容追加
    Response.End();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 11:18:00