0
我从ssrs报告中得到一个字节数组。然后我想将它保存在服务器上的Excel中以供进一步处理。 我能够在客户端浏览器上导出它,但是当尝试将文件保存在服务器上时,它会保存。但打开“excel无法打开文件时出现错误,因为文件格式或文件扩展名无效”。保存一个字节数组到excel文件
代码如下
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());
}
它不知道如何使用Infor的格式,deviceInfo等我跟Respose对象用来保存Excel在客户端浏览器。
这里是我如何写入Response对象。它工作的很好的gor响应对象。
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();
是Excel文件中的字节数组?你的代码看起来很像这个例子http://msdn.microsoft.com/en-us/library/ms252214(v=vs.90).aspx – Jodrell
这是ssrs报告。但我可以通过将其写入excel来将其转换为excel。查看更新的代码。 – rahul
如果你想用'File.WriteAllBytes'写一个'byte []'到一个文件,http://stackoverflow.com/a/381529/659190。显然,不是唯一的方法,但简单可靠。 – Jodrell