I am using C# API (HttpWebRequest) to export SSRS report as an excel spreadsheet. It downloads the report as Excel but when I try to open the spreadsheet, I get below error:
Excel cannot open the file because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
public void SaveFile(string reportURL, string fileName) { HttpWebRequest ioRequest; HttpWebResponse ioResponse; Stream ioResponseStream; FileStream ioFileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write); int ioCount = 1; byte[] bufferContainer = new byte[256]; ioRequest = (HttpWebRequest)WebRequest.Create(reportURL); ioRequest.Credentials = CredentialCache.DefaultCredentials; ioRequest.Timeout = 600000; ioRequest.Method = "GET"; ioResponse = (HttpWebResponse)ioRequest.GetResponse(); ioResponseStream = ioResponse.GetResponseStream(); do { ioCount = ioResponseStream.Read(bufferContainer, 0, 256); ioFileStream.Write(bufferContainer, 0, ioCount); } while (ioCount > 0); ioFileStream.Flush(); ioFileStream.Close(); }
I invoke the above method with destination file path and URL to report: http://abc:80/ReportServer?/displayprojects&rs:Command=Render&rs:Format=EXCEL
destination file path:
string fileName = varCollection["User::LocalFolder"].Value.ToString() + "displayprojects" + DateTime.Today.ToString("MMddyyyy") + ".xlsx";
If I paste the report URL into browser, it downloads excel file and I am able to open it.
Any pointers as to what I need to change in the implementation?