locked
Report Viewer Export Feature RRS feed

  • Question

  • does anyone know of anyway to execute the export to PDF feature of the reportviewer in code?

    i was thinking i could use reflection to do it but have not been able to.

    any ideas?
    Friday, November 18, 2005 1:03 AM

Answers

  • Call ReportViewer.LocalReport.Render() or ReportViewer.ServerReport.Render() (depending on which mode you are using).  Pass in "PDF" for the render format.
    Friday, November 18, 2005 5:31 PM

All replies

  • Call ReportViewer.LocalReport.Render() or ReportViewer.ServerReport.Render() (depending on which mode you are using).  Pass in "PDF" for the render format.
    Friday, November 18, 2005 5:31 PM
  • Hi Brian:

    Unfortunately, I am running into issues where this doesn't seem to be as simple as the code above. I can successfully just export as Excel, but not as PDF. When I do export as PDF and attempt to open up the file, I am told the PDF file is damaged.

    System info: VS2005, SQL Svr Dev 2005, RptViewer version 8.0.0.0

    I am creating an ASP.NET/C# application. The application is a simple page with the ReportViewer control embedded on the page, two links and two buttons. Each button tells the page to export/save the current viewed report as either excel or PDF. Excel works with the following code, PDF does not. What am I doing wrong?

    Thanks

    Michael

    public partial class _Default : System.Web.UI.Page
    {

    String m_currentReport;

    private void saveRptAs(String s_rptType)
    {

    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    // string deviceInfo;

    byte[] bytes = ReportViewer1.ServerReport.Render(
    s_rptType, null, out mimeType, out encoding, out extension,
    out streamids, out warnings);

    /*
    FileStream stream = File.OpenWrite(@"C:\Documents and Settings\michael.shorten\Local
    Settings\Temp\sample.pdf");
    stream.Write(bytes, 0, bytes.Length);
    stream.Close();
    */

    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition", "attachment; filename=sample."+extension);
    Response.BinaryWrite(bytes);
    Response.Flush();

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!(Request.QueryString["rpt"]== null))
    {
    m_currentReport = Request.QueryString["rpt"].ToString();
    showReport(m_currentReport);
    }

    }

    private void showReport(string sReportPath)
    {

    // Set Processing Mode

    ReportViewer1.ProcessingMode = ProcessingMode.Remote;

    // Set Report Server and path

    ReportViewer1.ServerReport.ReportServerUrl = new Uri(http://localhost/ReportServer);
    ReportViewer1.ServerReport.ReportPath = sReportPath;

    }

    protected void btnSaveExcel_Click(object sender, EventArgs e)
    {

    saveRptAs("Excel");

    }

    protected void btnSavePDF_Click(object sender, EventArgs e)
    {

    saveRptAs("PDF");

    }

    }

    Tuesday, December 27, 2005 1:55 PM
  • Does it render correctly if you write the PDF to a file on the server using the commented out code?

    Does it save correctly if you use the export dropdown on the built-in toolbar from the client?

    Tuesday, December 27, 2005 9:40 PM
  • Hello

    With your code, if you look generated file with a text editor there are some lines after "EOF" (script and html)
    Just put "Response.End();" after "Response.Flush();"

    JLE

    Wednesday, December 28, 2005 10:52 AM
  •  Brian Hartman - MSFT wrote:

    Does it render correctly if you write the PDF to a file on the server using the commented out code?

    Does it save correctly if you use the export dropdown on the built-in toolbar from the client?

    Brian,

    Thank you for replying. Yes, to both, I do get a proper PDF file.

    I believe the problem is the way I am streaming out the resultant bytes - it apparently works OK with Excel but not with PDF. Someone has written below a possible solution, I shall try it.

    Regards,

    Michael

    Wednesday, December 28, 2005 1:28 PM
  •  RenRen le casseur wrote:

    Hello

    With your code, if you look generated file with a text editor there are some lines after "EOF" (script and html)
    Just put "Response.End();" after "Response.Flush();"

    JLE

     

    Yup, that was it. Very strange, but it works. I hadn't seen the "Response.End" in the examples, but the again, most examples are saving to server first then downloading the file rather than streaming it directly to the user. Thanks!

    Regards,

    Michael

    Wednesday, December 28, 2005 3:26 PM
  • Another strange thing, with your code if you click on "Save", your document will be saved and you can open it. But if you click directely on "Open" I've an error (with Excell closed) "c:\documents and settings..... not found....". If Excell is opened, the document is loaded !?!? With PDF, I've a file not found error.

    An idea?

    JLE

     

    Wednesday, December 28, 2005 3:48 PM
  • Ren:

    I've not had that error that you describe. I always use the "Open" instead of "Save".  I leave Excel and Reader closed.

    Permissions issue perhaps?

    Good luck
    Regards,
    Michael Shorten

     

    Thursday, December 29, 2005 12:26 PM
  • Thanks for your replie Michael.
    I've just deleted internet temporary files. All is ok now.

    Regards,
    JLE

    Thursday, December 29, 2005 1:38 PM
  • I tried using the code provided in this thread to export the report to an Excel file. but, I get a compilation error:

    Compiler Error Message: CS1501: No overload for method 'Render' takes '7' arguments

    Source Error:

    Line 24: 		// string deviceInfo;
    Line 25: 
    Line 26: 		byte[] bytes = ReportViewer1.ServerReport.Render(
    Line 27: 		s_rptType, null, out mimeType, out encoding, out extension,
    Line 28: 		out streamids, out warnings);

    Can some please help me fix this.

    Wednesday, January 11, 2006 11:50 PM
  • Do you have a ReportViewer dropped on your web page?
    Wednesday, January 18, 2006 2:32 AM
  • If you are using the beta version of ReportViewer delete the fileNameExtension argument.
    Wednesday, January 18, 2006 3:08 AM
  • I am using beta version of ReportViewer. The reportviewer control has been dropped on the page. It gives an error ("Execution session has expired") when I use the export/refresh button on the reportviewer toolbar. I came to know that its the problem with the beta version of the reportviewer contrl. Hence, I am trying to export the report using an alternate method.

    Wednesday, January 18, 2006 3:47 PM
  • RenRen,

     

    Nice post.  Here I am find the same issue with Open option.  Save option is working fine.  I get "file not found" error.  I cleared the Temperory internet files too.  any ideas?

    Friday, May 11, 2007 5:59 AM
  • I don't know if the issue applies to the report viewer but I was experiencing a similar problem when downloading files through a stream.
    Found that IE6 didn't open the file directly (could not find it in the temp directory) but allowed one to save.

    I had to set the
    HTTPResponse's ContentType so that the meta data was correct.

    Not sure where one can set such in the reportviewer but maybe it's a starting point.
    Thursday, October 4, 2007 2:49 PM