This looks exactly like what I am needing myself! I am extremely new at SSRS Reporting, and still pretty green in .Net coding as well, so let me say thank you in advance for answering my simplistic questions.
I'm needing the VB version of this code, it looks like you just created a seperate class for this code? If so, how/where are you calling it from your Report Viewer codebehind? I know that Scott says to call it from the ReportViewer.LocalReport.Render()
function, but when I look at my method options for the ReportViewer class the only Render method I see is the PreRender method -- is this where I call it from?
My apologies, I asked my questions before I took time to study what was going on...after taking a closer look I think I have a good grasp for what's happening here. Since this is something I think I'll be doing fairly often -- sending the report output
straight to PDF, I decided to create a separate class for the code. Below is what I have so far.
I am stuck with one spot, and I'm hoping maybe you all can help me with it. My .rdlc is using a stored procedure with one parameter getting passed in, which is a Session variable. I set up the call to the stored procedure inside of the .rdlc. I've played
around with how to make this work in my function, but no matter what I do it keeps erroring out when calling the ReportViewer.LocalReport.Render method. I have tried commenting out the dataset, as you see below, and also commenting out the creation of the
ReportDataSource altogether. This feels like something simple that I'm overlooking. I'm going to keep playing around myself, but am hoping somebody can help save me some time!
Imports Microsoft.VisualBasic
Imports Microsoft.Reporting.WebForms
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class ReportCommon
Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String
' Setup DataSet
'Dim ds As New MyDataSetTableAdapters.DataTable1TableAdapter()
' Create Report DataSource
Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes") ', ds.GetData())
' Variables
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
Try
' Setup the report viewer object and get the array of bytes
Dim viewer As New ReportViewer()
viewer.ProcessingMode = ProcessingMode.Local
viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc"
viewer.LocalReport.DataSources.Add(rds)
Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
' Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
WebPage.Response.Buffer = True
WebPage.Response.Clear()
WebPage.Response.ContentType = mimeType
WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension)
WebPage.Response.BinaryWrite(bytes)
' create the file
' send it to the client to download
WebPage.Response.Flush()
Catch ex As Exception
Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message)
End Try
Return FileName
End Function
End Class
I did it, and I am so pleased with myself! It takes so little to make me happy
Below is the class I created to send the results of an SSRS report directly to a PDF file. I'm going to clean it up just a little bit more, so that the hard-coded stuff is passed in as parameters, but hopefully this will help anybody else who is looking
to do something similar...
Imports Microsoft.VisualBasic
Imports Microsoft.Reporting.WebForms
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Public Class ReportCommon
Inherits System.Web.UI.Page
Dim dc As New DataCommon
Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String
Dim ds As New DataSet
'Initialize Connection String
dc.Init("AssessmentTest")
'Call the Stored Proc and load the Dataset
dc.ExecStoredProc("sp_GetWritingMSBarcodes", "@GroupID", Session("GroupID"), SqlDbType.Int)
dc.sqlDA.Fill(ds, "DataTable1")
' Create Report DataSource
Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes", ds.Tables("DataTable1"))
' Variables
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
Try
' Setup the report viewer object and get the array of bytes
Dim viewer As New ReportViewer()
viewer.ProcessingMode = ProcessingMode.Local
viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc"
viewer.LocalReport.DataSources.Add(rds)
Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
' Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
WebPage.Response.Buffer = True
WebPage.Response.Clear()
WebPage.Response.ContentType = mimeType
WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension)
WebPage.Response.BinaryWrite(bytes)
' create the file
' send it to the client to download
WebPage.Response.Flush()
Catch ex As Exception
Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message)
End Try
Return FileName
End Function
End Class
could you please give this same code in vb.net windows application. because i have created RDLC report during i export it to excel format some columns are merged and data can be segregated in individula cell. so i could not upload it the our software.
if u have any solution for it please advice me...
thank you..i am waiting.
Export to Excel -Large empty row on top problemrdlc reportviewer PDF generate reports programmaticallyExcel reportview visual studio 2005
grandfunkj
Member
12 Points
22 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Jul 08, 2010 12:37 PM|LINK
Gentlemen,
This looks exactly like what I am needing myself! I am extremely new at SSRS Reporting, and still pretty green in .Net coding as well, so let me say thank you in advance for answering my simplistic questions.
I'm needing the VB version of this code, it looks like you just created a seperate class for this code? If so, how/where are you calling it from your Report Viewer codebehind? I know that Scott says to call it from the ReportViewer.LocalReport.Render() function, but when I look at my method options for the ReportViewer class the only Render method I see is the PreRender method -- is this where I call it from?
Thanks again for your help!
grandfunkj
Member
12 Points
22 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Jul 08, 2010 02:07 PM|LINK
My apologies, I asked my questions before I took time to study what was going on...after taking a closer look I think I have a good grasp for what's happening here. Since this is something I think I'll be doing fairly often -- sending the report output straight to PDF, I decided to create a separate class for the code. Below is what I have so far.
I am stuck with one spot, and I'm hoping maybe you all can help me with it. My .rdlc is using a stored procedure with one parameter getting passed in, which is a Session variable. I set up the call to the stored procedure inside of the .rdlc. I've played around with how to make this work in my function, but no matter what I do it keeps erroring out when calling the ReportViewer.LocalReport.Render method. I have tried commenting out the dataset, as you see below, and also commenting out the creation of the ReportDataSource altogether. This feels like something simple that I'm overlooking. I'm going to keep playing around myself, but am hoping somebody can help save me some time!
Imports Microsoft.VisualBasic Imports Microsoft.Reporting.WebForms Imports System.Data.SqlClient Imports System.Data Imports System.IO Public Class ReportCommon Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String ' Setup DataSet 'Dim ds As New MyDataSetTableAdapters.DataTable1TableAdapter() ' Create Report DataSource Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes") ', ds.GetData()) ' Variables 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 Try ' Setup the report viewer object and get the array of bytes Dim viewer As New ReportViewer() viewer.ProcessingMode = ProcessingMode.Local viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc" viewer.LocalReport.DataSources.Add(rds) Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings) ' Now that you have all the bytes representing the PDF report, buffer it and send it to the client. WebPage.Response.Buffer = True WebPage.Response.Clear() WebPage.Response.ContentType = mimeType WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension) WebPage.Response.BinaryWrite(bytes) ' create the file ' send it to the client to download WebPage.Response.Flush() Catch ex As Exception Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message) End Try Return FileName End Function End Classgrandfunkj
Member
12 Points
22 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Jul 08, 2010 02:39 PM|LINK
I did it, and I am so pleased with myself! It takes so little to make me happy
Below is the class I created to send the results of an SSRS report directly to a PDF file. I'm going to clean it up just a little bit more, so that the hard-coded stuff is passed in as parameters, but hopefully this will help anybody else who is looking to do something similar...
Imports Microsoft.VisualBasic Imports Microsoft.Reporting.WebForms Imports System.Data.SqlClient Imports System.Data Imports System.IO Public Class ReportCommon Inherits System.Web.UI.Page Dim dc As New DataCommon Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String Dim ds As New DataSet 'Initialize Connection String dc.Init("AssessmentTest") 'Call the Stored Proc and load the Dataset dc.ExecStoredProc("sp_GetWritingMSBarcodes", "@GroupID", Session("GroupID"), SqlDbType.Int) dc.sqlDA.Fill(ds, "DataTable1") ' Create Report DataSource Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes", ds.Tables("DataTable1")) ' Variables 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 Try ' Setup the report viewer object and get the array of bytes Dim viewer As New ReportViewer() viewer.ProcessingMode = ProcessingMode.Local viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc" viewer.LocalReport.DataSources.Add(rds) Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings) ' Now that you have all the bytes representing the PDF report, buffer it and send it to the client. WebPage.Response.Buffer = True WebPage.Response.Clear() WebPage.Response.ContentType = mimeType WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension) WebPage.Response.BinaryWrite(bytes) ' create the file ' send it to the client to download WebPage.Response.Flush() Catch ex As Exception Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message) End Try Return FileName End Function End ClassN_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Jul 08, 2010 04:54 PM|LINK
Congrats you are now a pro :)
mdnuhid
Member
6 Points
10 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Sep 19, 2010 12:39 PM|LINK
hi mr,
could you please give this same code in vb.net windows application. because i have created RDLC report during i export it to excel format some columns are merged and data can be segregated in individula cell. so i could not upload it the our software. if u have any solution for it please advice me...
thank you..i am waiting.
Export to Excel -Large empty row on top problem rdlc reportviewer PDF generate reports programmatically Excel reportview visual studio 2005
mdnuhid
Member
6 Points
10 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Sep 19, 2010 12:40 PM|LINK
what u got it or not
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Sep 24, 2010 05:50 PM|LINK
Do the same thing. The WinForms version of the reportviewer has the same Render() function for ServerReports or LocalReports.
Masterr
Member
10 Points
5 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Feb 10, 2011 10:08 AM|LINK
how to create gridview grouping
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Feb 14, 2011 06:45 PM|LINK
in an RDLC file? or in the <asp:GridView /> control?
Masterr
Member
10 Points
5 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Feb 15, 2011 03:10 AM|LINK
GridView Control