Exportando um relatório .rdlc para PDF

by Stiven 4. março 2009 09:59

Para aqueles que gostam de relatórios em PDF e não querem passar pelo processo de selecionar o formato do relatório a ser exportado no ReportViewer, utilize a função abaixo:

    public static void ExportReportToPDF(HttpContext context, Microsoft.Reporting.WebForms.ReportViewer rViewer)

    {

        Microsoft.Reporting.WebForms.Warning[] warnings;

        string[] streamids;

        string mimeType = "";

        string encoding = "";

        string extension = "";

        byte[] bytes = rViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);

 

        context.Response.Buffer = true;

        context.Response.Clear();

        context.Response.ContentType = mimeType;

        context.Response.AddHeader("content-disposition", "outline; filename=myfile." + extension);

        context.Response.BinaryWrite(bytes);

        context.Response.Flush();

    }

 

Para exportar para excel, basta alterar de:

 

 byte[] bytes = rViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);

 

para:

 

 byte[] bytes = rViewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);

Tags:

ASP.Net | Dicas

Comentários (2) -

MArcos
MArcos Brazil
16/9/2009 10:46:29 #

E como eu chamo a função? Com quais parametros?

Stiven
Stiven Brazil
22/9/2009 15:11:06 #

É simples, na página .aspx

<form id="form1" runat="server">
        <rsweb:ReportViewer ID="reportViewer" ShowRefreshButton="false" runat="server" Font-Names="Verdana"
            Font-Size="8pt" Height="842px" Width="100%" Style="margin-bottom: 10px; padding-bottom: 20px">
        </rsweb:ReportViewer>
    </form>

e no .cs:

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable data = meuDataTable;

        reportViewer.LocalReport.EnableExternalImages = true;
        reportViewer.LocalReport.DataSources.Clear();
        reportViewer.LocalReport.DataSources.Add(new ReportDataSource("MeuDataSource", data));
        reportViewer.LocalReport.ReportPath = @"MeuReport.rdlc";

        reportViewer.DataBind();
        reportViewer.ExportContentDisposition = ContentDisposition.AlwaysInline;
        Utils.ExportReportToPDF(Context, reportViewer);
    }

Comentar




  Country flag
biuquote
  • Comentário
  • Pré-visualização
Loading


Sobre o autor



Meu nome é Stiven Fabiano da Câmara e sou desenvolvedor de software especializado na plataforma .NET da Microsoft, utilizando a linguagem Visual C#.