2010-07-27 5 views
0

Привет, может кто-нибудь помочь мне в этом.Экспорт отчета rdlc в pdf при нажатии кнопки

У меня есть отчет RDLC, отображаемый на моей веб-странице с помощью asp.net И C# .net Я хочу экспортировать его в PDF при нажатии кнопки.

Пожалуйста, помогите мне?

Спасибо

ответ

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") 
+1

Требуется доступ на запись в файловую систему. Почему бы просто не прочитать байты в MemoryStream, очистить поток ответов, правильно настроить заголовки HTTP и напрямую передать файл клиенту? – tomfanning

+0

Yeppers, это сработает. Я понял, что, поскольку он уже был прочитан в байтах, было бы очевидно, что это был вариант. – DevDave

Смежные вопросы