在asp.net MVC中,我可以执行以下操作,这将打开一个流:
Stream strm1 = GenerateReport(Id); return File(strm1,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","Report_" + reportId.ToString() + ".xlsx");
注意我是如何传递strm1这是一个流.我可以将其命名为Report_ … xlsx,就像上面的例子所示.
是否有类似的方法使用c#与asp.net Web表单执行此操作.
解决方法
如果文件位于您的网站文件夹中,则可以使用
TransmitFile或
WriteFile.
string fileName = string.Format("Report_{0}.xlsx",reportId); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition",string.Format("attachment; filename={0}",fileName)); Response.TransmitFile(fileName); Response.End();
流
如果您的数据已经在内存中,则需要使用此方法将响应写入块中.
Stream stm1 = GenerateReport(Id); Int16 bufferSize = 1024; byte[] buffer = new byte[bufferSize + 1]; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition",string.Format("attachment; filename=\"Report_{0}.xlsx\";",reportId)); Response.BufferOutput = false; int count = stm1.Read(buffer,bufferSize); while (count > 0) { Response.OutputStream.Write(buffer,count); count = stm1.Read(buffer,bufferSize); }