so when i use in my code "rpt_fouten.ReportViewer1.LocalReport.Render " because rpt_fouten is name of the form, en reportviewer1 is name van de reportviewer.
Does the then know that rpt_fouten.rdlc is behind this reportviewer?
OK my code works BUT
I don't get any data in my table , where do I have to bind my data to the report before I render it ?
Public Sub createpdf(ByVal filename As String)
Dim reportType As String = "PDF"
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
'Dim fileNameExtension As String
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
Okay, even though this is an ASP.NET website and NOT windows forms, several people have asked for help converting this to Windows forms, so here is a class I wrote in c# that will perform the actions for you.
This class has 2 basic functions that will either auto save the file to a specified location, or open a save file dialog for the user to save it that way. This class also allows you to export to PDF, Excel, as well as Word (Word ONLY WORKS WITH REPORT VIEWER
2010!!!)
To use the class just copy and paste it to your project, make sure you have the necessary Microsoft.Reporting references, and you should be good. Here is how to use the class:
// To automatically save the file to the file system
PDF_Generator g = new PDF_Generator();
g.DataSourceName = "YourDataSourceNameHere";
g.DataSourceValue = YourDatahere;
g.FileStorageLocation = "C:\\MyAutoSavedReports";
g.ReportPath = "YourAssemblyNamehere.YourReportName.rdlc";
g.AutoSaveFile(PDF_Generator.ReportType.<type you want here>, "file name + extension here");
// To ask for a save file dialog prompt
PDF_Generator g = new PDF_Generator();
g.DataSourceName = "YourDataSourceNameHere";
g.DataSourceValue = YourDatahere;
g.ReportPath = "YourAssemblyNamehere.YourReportName.rdlc";
g.SendToSaveDialog(PDF_Generator.ReportType.<type you want here>, "file name + extension here");
Here is the class itself, it is provided with no warranties and is given AS IS.
using System.IO;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace TestRDLC
{
public class PDF_Generator
{
#region Constructors
public PDF_Generator()
{
DataSourceName = string.Empty;
DataSourceValue = null;
ReportPath = string.Empty;
FileStorageLocation = string.Empty;
}
#endregion
#region Public Methods
public byte[] GetFile(string reportType)
{
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
ReportDataSource rds = new ReportDataSource(DataSourceName, DataSourceValue);
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportEmbeddedResource = ReportPath;
viewer.LocalReport.DataSources.Add(rds);
return viewer.LocalReport.Render(reportType, null, out mimeType, out encoding, out extension, out streamIds, out warnings);
}
public void AutoSaveFile(ReportType type, string fileName)
{
if (!string.IsNullOrEmpty(DataSourceName) &&
DataSourceValue != null &&
!string.IsNullOrEmpty(ReportPath) &&
!string.IsNullOrEmpty(FileStorageLocation))
{
byte[] file = GetFile(type.ToString());
FileStream stream = new FileStream(FileStorageLocation + "\\" + fileName, FileMode.Create, FileAccess.Write);
stream.Write(file, 0, file.Length);
stream.Close();
}
else
{
MessageBox.Show("Required information missing before your file can be automatically processed", "Error creating requested file");
}
}
public void SendToSaveDialog(ReportType type, string fileName)
{
if (!string.IsNullOrEmpty(DataSourceName) &&
DataSourceValue != null &&
!string.IsNullOrEmpty(ReportPath))
{
byte[] file = GetFile(type.ToString());
FileStream stream;
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "All files (*.*)|*.*";
dialog.RestoreDirectory = true;
dialog.FileName = fileName;
if (dialog.ShowDialog() == DialogResult.OK)
{
if ((stream = (FileStream)dialog.OpenFile()) != null)
{
stream.Write(file, 0, file.Length);
stream.Close();
}
}
}
else
{
MessageBox.Show("Required information missing before your file can be processed", "Error creating requested file");
}
}
#endregion
#region Properties
public string DataSourceName { get; set; }
public object DataSourceValue { get; set; }
public string ReportPath { get; set; }
public string FileStorageLocation { get; set; }
#endregion
#region Enum
public enum ReportType
{
PDF,
Excel,
Word
}
#endregion
}
}
ok I used your system but I still don't get any data in my pdf report.
this is my code I use
what am I doing wrong
Public Sub createpdf()
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
' gegevens op vragen
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from View_overzicht_fouten"
adapter = New SqlDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
You need to either render the data from your current report viewer object on the form, or you need to make sure you put the ReportDataSource on the new page to provide the data.
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
rpt_fouten.repview_fouten.LocalReport.DataSources.Add(rds)
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 08:42 AM|LINK
2 options
1) You can just pass in the form where the ReportViewer is contained to the new form where the button is
or
2) You can just create a new ReportViewer in code on the new page, either way will work.
gunterhoflac...
Member
14 Points
8 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 08:46 AM|LINK
so when i use in my code "rpt_fouten.ReportViewer1.LocalReport.Render " because rpt_fouten is name of the form, en reportviewer1 is name van de reportviewer.
Does the then know that rpt_fouten.rdlc is behind this reportviewer?
OK my code works BUT
I don't get any data in my table , where do I have to bind my data to the report before I render it ?
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 08:48 AM|LINK
Let me see if I can create something really quick to help you out. Hang on...
gunterhoflac...
Member
14 Points
8 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 08:51 AM|LINK
code already works now, BUT no data in my tabel
Public Sub createpdf(ByVal filename As String)
Dim reportType As String = "PDF"
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
'Dim fileNameExtension As String
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
'Render the report
renderedBytes = rpt_fouten.ReportViewer1.LocalReport.Render(reportType, Nothing, mimeType, encoding, Nothing, Nothing, warnings)
Dim fs As FileStream =
System.IO.File.Create("c:\ascii\" & filename & ".pdf")
fs.Write(renderedBytes, 0, renderedBytes.Length)
fs.Close()
End Sub
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 09:45 AM|LINK
Okay, even though this is an ASP.NET website and NOT windows forms, several people have asked for help converting this to Windows forms, so here is a class I wrote in c# that will perform the actions for you.
This class has 2 basic functions that will either auto save the file to a specified location, or open a save file dialog for the user to save it that way. This class also allows you to export to PDF, Excel, as well as Word (Word ONLY WORKS WITH REPORT VIEWER 2010!!!)
To use the class just copy and paste it to your project, make sure you have the necessary Microsoft.Reporting references, and you should be good. Here is how to use the class:
Here is the class itself, it is provided with no warranties and is given AS IS.
using System.IO; using System.Windows.Forms; using Microsoft.Reporting.WinForms; namespace TestRDLC { public class PDF_Generator { #region Constructors public PDF_Generator() { DataSourceName = string.Empty; DataSourceValue = null; ReportPath = string.Empty; FileStorageLocation = string.Empty; } #endregion #region Public Methods public byte[] GetFile(string reportType) { Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = string.Empty; string extension = string.Empty; ReportDataSource rds = new ReportDataSource(DataSourceName, DataSourceValue); ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportEmbeddedResource = ReportPath; viewer.LocalReport.DataSources.Add(rds); return viewer.LocalReport.Render(reportType, null, out mimeType, out encoding, out extension, out streamIds, out warnings); } public void AutoSaveFile(ReportType type, string fileName) { if (!string.IsNullOrEmpty(DataSourceName) && DataSourceValue != null && !string.IsNullOrEmpty(ReportPath) && !string.IsNullOrEmpty(FileStorageLocation)) { byte[] file = GetFile(type.ToString()); FileStream stream = new FileStream(FileStorageLocation + "\\" + fileName, FileMode.Create, FileAccess.Write); stream.Write(file, 0, file.Length); stream.Close(); } else { MessageBox.Show("Required information missing before your file can be automatically processed", "Error creating requested file"); } } public void SendToSaveDialog(ReportType type, string fileName) { if (!string.IsNullOrEmpty(DataSourceName) && DataSourceValue != null && !string.IsNullOrEmpty(ReportPath)) { byte[] file = GetFile(type.ToString()); FileStream stream; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "All files (*.*)|*.*"; dialog.RestoreDirectory = true; dialog.FileName = fileName; if (dialog.ShowDialog() == DialogResult.OK) { if ((stream = (FileStream)dialog.OpenFile()) != null) { stream.Write(file, 0, file.Length); stream.Close(); } } } else { MessageBox.Show("Required information missing before your file can be processed", "Error creating requested file"); } } #endregion #region Properties public string DataSourceName { get; set; } public object DataSourceValue { get; set; } public string ReportPath { get; set; } public string FileStorageLocation { get; set; } #endregion #region Enum public enum ReportType { PDF, Excel, Word } #endregion } }gunterhoflac...
Member
14 Points
8 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 11:12 AM|LINK
ok I used your system but I still don't get any data in my pdf report.
this is my code I use
what am I doing wrong
Public Sub createpdf()
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
' gegevens op vragen
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from View_overzicht_fouten"
adapter = New SqlDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
rpt_fouten.repview_fouten.ProcessingMode = ProcessingMode.Local
rpt_fouten.repview_fouten.LocalReport.ReportEmbeddedResource = "klantprogramma.rpt_fouten.rdlc"
rpt_fouten.repview_fouten.LocalReport.DataSources.Add(rds)
rpt_fouten.repview_fouten.LocalReport.Refresh()
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
'Render the report
renderedBytes = rpt_fouten.repview_fouten.LocalReport.Render("PDF", Nothing, mimeType, encoding, Nothing, Nothing, warnings)
Dim tijdcode As String = jaarnaar8(Date.Now())
MsgBox(tijdcode)
Dim fs As FileStream =
System.IO.File.Create("c:\ascii\rpt_fouten" & tijdcode & ".pdf")
fs.Write(renderedBytes, 0, renderedBytes.Length)
fs.Close()
End Sub
N_EvilScott
Star
8179 Points
1466 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 25, 2011 10:56 PM|LINK
You need to either render the data from your current report viewer object on the form, or you need to make sure you put the ReportDataSource on the new page to provide the data.
gunterhoflac...
Member
14 Points
8 Posts
Re: RDLC - Export directly to Excel or PDF from codebehind
Aug 26, 2011 09:12 AM|LINK
Don't I add the data to the report here ?
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
rpt_fouten.repview_fouten.LocalReport.DataSources.Add(rds)
with this code??
or do i make a mistake?