0

We're using SSRS 2017 as our report server with ReportService2010.asmx and ReportExecution2005.asmx as the web references. When I export the report to Excel in the reporting server or project, it exports to .xlsx file and I'm able to open it without any issues but when I export the report to .xlsx (EXCELOPENXML) using a web forms app (C#), it exports the report but shows an alert "We found a problem with some content..." (see image) when opening the file. If I say, "Yes", excel repairs the file and shows the correct data. Error log is also not very helpful.

I don't know what is causing the web forms app to export an corrupted .xlsx file. Any help is appreciated. Thanks.

enter image description here

enter image description here

Ram
  • 661
  • 2
  • 12
  • 37
  • Have you checked the XML in the Excel to see if there's a difference? Maybe an unprintable character? https://christianspecht.de/2014/01/14/excel-found-unreadable-content-when-exporting-a-reporting-services-report/ – Hannover Fist Jun 16 '21 at 17:05
  • @HannoverFist Yes, but don't see any difference. – Ram Jun 16 '21 at 18:31
  • I had a similar issue when data in my report exceeded (in t-sql) Decimal(14,6) I think. The way Excel and SQL handle the number is different. I ended up writing a small stored proc that took the output for the report and converted all the relevant columns to a compatible numeric type. If you are interested, I will post the code (it's a small stored proc) and explain how to use it. If all your report data is low scale (less than 6 decimal places) then this is unlikely to be your issue. – Alan Schofield Jun 16 '21 at 21:51
  • @AlanSchofield I don't have any decimals in my report so I think it's a different issue – Ram Jun 17 '21 at 13:18

2 Answers2

0

I had to Flush and Close the HttpResponse request for it to work.

response.OutputStream.Flush();
response.OutputStream.Close();
response.Flush();
response.Close();
Ram
  • 661
  • 2
  • 12
  • 37
0

This is what worked for me.

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
            }

        }
drzounds
  • 369
  • 3
  • 16