How to render client report definition files (.rdlc) directly to the Response stream without preview

本文介绍了一种在ASP.NET中不使用ReportViewer控件而直接将RDLC报告定义文件渲染到Response流的方法。这种方法适用于不需要交互操作的报告场景,并提供了将报告渲染为PDF格式的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://weblogs.asp.net/rajbk/archive/2006/03/02/How-to-render-client-report-definition-files-_28002E00_rdlc_2900_-directly-to-the-Response-stream-without-preview.aspx

 

Note: I cover this technique in a more recent post here : Rendering an RDLC directly to the Response stream in ASP.NET MVC

A ReportViewer control is normally used to open a report definition file, process it and load it into the viewing area.

The simple method below allows you to render the report directly to the response stream without using the ReportViewer control. This might be useful in cases where you want to render a non interactive report.

The example below renders the report in PDF format. The other report types available when using the LocalReport.Render method are “Excel”and “Image”.

///<summary>

/// References:

///</summary>

private void RenderReport() {

    LocalReport localReport = new LocalReport();

    localReport.ReportPath = Server.MapPath("~/Report.rdlc");
  

    //A method that returns a collection for our report

    //Note: A report can have multiple data sources

    List<Employee> employeeCollection = GetData();

    //Give the collection a name (EmployeeCollection) so that we can reference it in our report designer

    ReportDataSource reportDataSource = new ReportDataSource("EmployeeCollection", employeeCollection);

    localReport.DataSources.Add(reportDataSource);

    string reportType = "PDF";

    string mimeType;

    string encoding;

    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType

    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx

    string deviceInfo =

    "<DeviceInfo>" +

    <OutputFormat>PDF</OutputFormat>" +

    <PageWidth>8.5in</PageWidth>" +

    <PageHeight>11in</PageHeight>" +

    <MarginTop>0.5in</MarginTop>" +

    <MarginLeft>1in</MarginLeft>" +

    <MarginRight>1in</MarginRight>" +

    <MarginBottom>0.5in</MarginBottom>" +

    "</DeviceInfo>";

    Warning[] warnings;

    string[] streams;

    byte[] renderedBytes;

    //Render the report

    renderedBytes = localReport.Render(

        reportType,

        deviceInfo,

        out mimeType,

        out encoding,

        out fileNameExtension,

        out streams,

        out warnings);

    //Clear the response stream and write the bytes to the outputstream

    //Set content-disposition to "attachment" so that user is prompted to take an action

    //on the file (open or save)

    Response.Clear();

    Response.ContentType = mimeType;

    Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);

    Response.BinaryWrite(renderedBytes);

    Response.End();

}

Note that if you change the ReportType in the Render method, you will also have to change the DeviceInfo settings.       

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值