按钮点击将rdlc报告导出为pdf

时间:2010-07-27 12:41:19

标签: asp.net reporting-services

任何人都可以帮助我。

我使用asp.net和C#.net在我的网页上显示了RDLC报告我希望在点击按钮时将其导出为PDF。

请你能帮帮我吗?

由于

1 个答案:

答案 0 :(得分:1)

我刚才做过这样的事情。下面是我在页面的page_load事件中使用的代码。它在VB中并不是世界上最好的代码,但可能会帮助您获得解决方案..

    Dim jobid As Integer = Request("jobid")
    Dim rv As New Microsoft.Reporting.WebForms.ReportViewer
    Dim r As String = "apps/Reports/legal_document.rdlc"
    Dim ds As New jobmanagerTableAdapters.JobInformationTableAdapter
    Dim ds2 As New ordermanagementTableAdapters.RecoveryItemsInformationTableAdapter
    Dim ds3 As New expensemanagerTableAdapters.tbl_expensesTableAdapter
    Dim ds4 As New ordermanagementTableAdapters.tbl_orders_collection_itemsTableAdapter
    Dim ds5 As New attachmentsmanagerTableAdapters.tbl_attachmentsTableAdapter
    Dim ds6 As New notesmanagerTableAdapters.tbl_notesTableAdapter
    Dim ds7 As New payments_managerTableAdapters.tbl_paymentsTableAdapter


    Dim rptSource1 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource2 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource3 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource4 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource5 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptSource6 As New Microsoft.Reporting.WebForms.ReportDataSource
    Dim rptsource7 As New Microsoft.Reporting.WebForms.ReportDataSource

    rptSource1.Name = "jobmanager_JobInformation"
    rptSource1.Value = ds.GetJobInfobyJobID(jobid)

    rptSource2.Name = "ordermanagement_RecoveryItemsInformation"
    rptSource2.Value = ds2.GetRecoveryItemsbyJobIDOrderID(jobid, 0)

    rptSource3.Name = "expensemanager_tbl_expenses"
    rptSource3.Value = ds3.GetExpensesbyJobIDOrderID(jobid, 0)

    rptSource4.Name = "ordermanagement_tbl_orders_collection_items"
    rptSource4.Value = ds4.GetDataByJobIDOrderID(jobid, 0)

    rptSource5.Name = "attachmentsmanager_tbl_attachments"
    rptSource5.Value = ds5.GetAllAttachmentsbyJobID(jobid)

    rptSource6.Name = "notesmanager_tbl_notes"
    rptSource6.Value = ds6.GetAllNotesbyJobID(jobid)

    rptsource7.Name = "payments_manager_tbl_payments"
    rptsource7.Value = ds7.GetPaymentsbyJobID(jobid)


    rv.LocalReport.DataSources.Clear()

    rv.LocalReport.ReportPath = r.ToString
    rv.LocalReport.DataSources.Add(rptSource1)
    rv.LocalReport.DataSources.Add(rptSource2)
    rv.LocalReport.DataSources.Add(rptSource3)
    rv.LocalReport.DataSources.Add(rptSource4)
    rv.LocalReport.DataSources.Add(rptSource5)
    rv.LocalReport.DataSources.Add(rptSource6)
    rv.LocalReport.DataSources.Add(rptsource7)

    'Page.Controls.Add(rv)

    Dim warnings As Warning() = Nothing
    Dim streamids As String() = Nothing
    Dim mimeType As String = Nothing
    Dim encoding As String = Nothing
    Dim extension As String = Nothing
    Dim bytes As Byte()



    'Get folder on web server from web.config
    Dim FolderLocation As String
    FolderLocation = Server.MapPath("reports")


    'First delete existing file
    Dim filepath As String = FolderLocation & "\legal.PDF"
    File.Delete(filepath)



    'Then create new pdf file
    bytes = rv.LocalReport.Render("PDF", Nothing, mimeType, _
        encoding, extension, streamids, warnings)


    Dim fs As New FileStream(FolderLocation & "\legal.PDF", FileMode.Create)
    fs.Write(bytes, 0, bytes.Length)
    fs.Close()

    Response.Redirect("reports/legal.pdf")