'Save a byte array to excel file
I get a byte array from ssrs report. Then I want to save it in Excel on server for further processing. I am able to export it on client browser but when try to save the file on server it saves. But gives the error while opening "excel can not open the file because the file format or file extension is not valid".
code is as follows
Microsoft.Reporting.WebForms.ReportViewer rview = new Microsoft.Reporting.WebForms.ReportViewer();
//Web Address of your report server (ex: http://rserver/reportserver)
rview.ServerReport.ReportServerUrl = new Uri("http://rserver/reportserver");
rview.ServerReport.ReportPath = "/Report Project2/Comment";
string mimeType, encoding, extension, deviceInfo;
string[] streamids;
Microsoft.Reporting.WebForms.Warning[] warnings;
string format = "Excel"; //Desired format goes here (PDF, Excel, or Image)
deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";
byte[] bytes = rview.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
try
{
System.IO.FileStream _FileStream = new System.IO.FileStream(Server.MapPath("output.xlsx"), System.IO.FileMode.Create, System.IO.FileAccess.Write);
_FileStream.Write(bytes, 0, bytes.Length);
//_FileStream.Close();
}
catch (Exception _Exception)
{
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
It don't know how to use infor format,deviceInfo etc. that i use with Respose object to save excel on client browser.
here is how I write to Response object.It works fine gor response object.
Response.Clear();
if (format == "PDF")
{
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "filename=output.pdf");
}
else if (format == "Excel")
{
Response.ContentType = "application/excel";
Response.AddHeader("Content-disposition", "filename=output.xls");
}
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
Response.Close();
Solution 1:[1]
Please change the extension to xls. The xlsx extension is for format EXCELOPENXML and for xls the format in EXCEL
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Toby Varghese |
