如何更改此类以返回我可以发送回浏览器的流?
我正在尝试使用报表查看器创建Excel文件,并将其发送回使用浏览器的客户端,
public class WebReportRenderer : IDisposable
{
private ReportViewer rvInstance;
private IList<Stream> m_streams;
public WebReportRenderer(string reportPath, string downloadFileName, string dataSetName, DataTable dataTable)
{
rvInstance = new ReportViewer();
rvInstance.LocalReport.ReportPath = reportPath;
rvInstance.ProcessingMode = ProcessingMode.Local;
rvInstance.LocalReport.DataSources.Add(new ReportDataSource(dataSetName, dataTable));
rvInstance.LocalReport.Refresh();
}
public void RenderToBrowserExcel()
{
var callback = new CreateStreamCallback(CreateWebBrowserStream);
Warning[] warnings;
m_streams = new List<Stream>();
rvInstance.LocalReport.Render("EXCELOPENXML", null, callback, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private Stream CreateWebBrowserStream(string name, string extension, System.Text.Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + "." + extension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
public void Dispose()
{
if (this.rvInstance != null) this.rvInstance.Dispose();
}
}
现在,如果我致电WebReportRenderer(... all para).RenderToBrowserExcel()
,它会创建一个流并将其添加到m_streams
,但我不确定是否应将其公开,然后从中获取结果?
不确定设计此课程的最佳方法是什么。我还需要在WCF方法或此报告渲染器类的某个地方添加以下内容,
var outGoingResponse = WebOperationContext.Current.OutgoingResponse;
outGoingResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
outGoingResponse.Headers["Content-Disposition"] = "attachment";
免责声明:
我从这里拿了一些代码 - http://www.danielroot.info/2009/06/how-to-render-reporting-services.html