I need a code to export the data showing in my reportviewer to a .pdf file.
can anyone help me?
Printable View
I need a code to export the data showing in my reportviewer to a .pdf file.
can anyone help me?
1.You need add reference of PDF file..
2. Import that reference in Source code.
Add ReportViewer control to form
Set report as your report
Click dropdown of button with little save disk
Select Pdf
I know about the save as pdf button embedded in the reportviewer, but I need to do this with my own code.
I did find this:
vb.net Code:
ReportViewer1.LocalReport.Render(.....
Now I just need to figure out how it works
add these includes:
on your button click event add:Code:using System.Configuration;
using Microsoft.Reporting.WinForms;
using System.IO;
You can also change "PDF" to "EXCEL" and "output.pdf" to "output.xls" for an excel reportCode:Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rvReport.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
FileStream fs = new FileStream(@"c:\output.pdf", FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
You can also change "PDF" to "IMAGE" and "output.pdf" to "output.jpg" for an image report
don't forget to catch exceptions using the try catch cos you never know
I got it working like this.
vb.net Code:
Private Sub ExportRPTtoPDF(ByVal FolderLocation As String, ByVal FileName As String) 'ADD THE FOLLOWING: ' Imports System.IO ' Imports Microsoft.Reporting.WinForms 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() 'First delete existing file Dim filepath As String = FolderLocation & "\" & FileName & ".PDF" File.Delete(filepath) 'Then create new pdf file bytes = rvInv.LocalReport.Render("PDF", Nothing, mimeType, _ encoding, extension, streamids, warnings) Dim fs As New FileStream(FolderLocation & "\" & FileName & ".PDF", FileMode.Create) fs.Write(bytes, 0, bytes.Length) fs.Close() End Sub
Thank you for your help.