none
ReportViewer direct export RRS feed

  • Question

  • Is is possible to export directly to Excel or PDF from a codebehind file without showing the reportviewer first and without the user having to click the export TextButton. So that only the generating functionality is used?
    Monday, June 11, 2007 9:11 AM

All replies

  • Yes it is possible.  You need to call the report with the full URL Access instructions using a standard HTTP call.  Here is an example I wrote for somebody else on a different forum -- although, depending on a couple of things, you may need to use System.Net.WebRequest instead of System.Net.HttpWebRequest as I did it here:

     

    Below is some sample script that you could use. Just remember that, where I've hard-coded a sample report URL , you will actually be building it up based on package variables.  And that includes your params. 
     

    Notice how the URL includes a query string representing the path of the report and two options (in this case, rs:Command and rs:Format).  These are arguments that belong to the report engine, and you can see it because of their rs: prefix. 

    But if you had (say) a parameter for this report such as PopulationGreaterThan (the report in my example URL is based on a table of cities in the MySQL standard sample database), you could add this right onto the end of the URL:
     
    &PopulationGreaterThan=50000
     
    ... got that? I give users URLs that include report params all the time... So, here's the code... Have fun.. >L<
     
     
    Code Snippet
    Dim ox As System.Net.HttpWebRequest =  _
    'just substitute &rs:Format=PDF for &rs:format=EXCEL in the line above for a PDF

    ox.UseDefaultCredentials = True
    ' the line above may not work for you, you may have to provide credential information with more work

    Dim oy As System.Net.HttpWebResponse = ox.GetResponse()
    Dim raw As System.IO.FileStream = New System.IO.FileStream("c:\temp\x.xls", IO.FileMode.Create)
    ' for a PDF, change the extension in the line above, along with changing its
    ' name and folder based on (obviously!) more package vars
    Dim buffer(1024) As Byte
    Dim rs As System.IO.Stream = oy.GetResponseStream()
    Dim read As Integer = rs.Read(buffer, 0, buffer.Length)
    While (read > 0)
       raw.Write(buffer, 0, read)
       read = rs.Read(buffer, 0, buffer.Length)
    End While
    oy.Close()
    raw.Close()
    raw.Dispose()
    rs.Close()
    rs.Dispose()
    oy =
    Nothing
    ox = Nothing
    raw = Nothing
    rs = Nothing

     

    Monday, June 11, 2007 3:10 PM
  • You can also do this with a local report.  See relative code pieces below:

     

     

    protected void Page_Load(object sender, EventArgs e)

    {

       SetupReport(RptView1);

       RenderReport(RptView1, Response);

    }

     

    private void SetupReport(ReportViewer reportViewer)

    {

      DataSet _ReportData = GetReportTrainingOfficialRecord(_PeopleID);

     

       reportViewer.LocalReport.DataSources.Clear();

       reportViewer.LocalReport.ReportPath = @"Reports\ReportTrainingOfficialRecord.rdlc";

       reportViewer.LocalReport.DataSources.Add(new ReportDataSource("PersonInfo", _ReportData.Tables[0]));

     

       reportViewer.LocalReport.Refresh();

    }

     

    private void RenderReport(ReportViewer reportViewer, HttpResponse response)

    {

    Warning[] warnings;

    string[] streamids;

    string mimeType;

    string encoding;

    string extension;

    byte[] bytes = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding

    , out extension, out streamids, out warnings);

    response.Clear();

    response.ContentType = mimeType;

    response.AppendHeader("content-Disposition", "inline: filename=TrainingOfficialRecord." + extension);

    response.BinaryWrite(bytes);

    response.End();

    }

    Monday, June 11, 2007 8:21 PM