2

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();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
rahul
  • 31
  • 1
  • 1
  • 6
  • is the byte array an Excel file? Your code looks very much like this example http://msdn.microsoft.com/en-us/library/ms252214(v=vs.90).aspx – Jodrell Oct 11 '13 at 09:42
  • it is the ssrs report. But I am able to convert it to excel by writing it to excel. See updated code. – rahul Oct 11 '13 at 09:45
  • If you want to write a `byte[]` to a file use `File.WriteAllBytes`, http://stackoverflow.com/a/381529/659190. Obviously, not the only way but simple and reliable. – Jodrell Oct 11 '13 at 09:49
  • @Jodrell in msdn link it convert it to .xls format. Can it be converted into .xlsx format. – rahul Oct 11 '13 at 09:50
  • What version of SSRS are you using? If you have 2012+ then you'll have compatability problems with Excel 2003. Otherwise, 2007+ versions of Excel should be compatible with legacy Excel files. – Jodrell Oct 11 '13 at 10:13
  • http://technet.microsoft.com/en-us/library/dd255234.aspx – Jodrell Oct 11 '13 at 10:13

1 Answers1

0

Please change the extension to xls. The xlsx extension is for format EXCELOPENXML and for xls the format in EXCEL