0

您好,感谢您的关注。

我有一些 .net 代码使用 SSRS 报告执行服务为用户下载报告作为 xlsx 文件。

它工作正常,报告中的所有内容都存在并得到说明。

不过有一件烦人的事。在 Excel 365 中打开时,它会弹出一个提示:

我们发现“theReport.xlsx”中的某些内容存在问题您希望我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击是

当我单击是时,它表示工作簿已修复并且报告看起来正常。

它在日志文件中没有给出任何指示它修复了什么,只是它被修复了。

请看一下我的代码。也许在打开 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);

            Response.ClearContent();

          
                if (rptFormat == "EXCELOPENXML")
                {
               
                Response.AppendHeader("content-disposition", "attachment; filename=" + strRptFileName + ".xlsx");
                
                Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            }

            Response.BinaryWrite(result);
            Response.Flush();

        }
4

1 回答 1

0

这终于奏效了。

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);

            Response.ClearContent();
            Response.Clear();
            
             
                if (rptFormat == "EXCELOPENXML")
                {
                 Response.AppendHeader("content-disposition", "attachment; filename=" + strRptFileName + ".xlsx");

                 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
               
            }

            Response.BinaryWrite(result);
            Response.Flush();
            Response.SuppressContent = true;
            try
            { 
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (ThreadAbortException ex)
            {
                //ignore
            }

        }
于 2021-11-09T20:15:06.613 回答