Render multiple reports in one report viewer (rdlc) - c#

Using ReportViewer i want to render multiple reports in one pdf using rdlc file.
Byte pdfByte = Byte();
pdfByte = ReportViewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extensions, stream, warning);
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=myfile." + extension);
Response.BinaryWrite(pdfByte);
Response.Flush();
Response.End();
This only generate only one report in pdf. But i want to get multiple reports rendered in one pdf file.
Please help me.

The easiest way to do this would be to design all the reports to be sub-reports of one main report. Then, by default all reports will be rendered in one PDF.
There's a couple of videos demonstrating sub-reports here: Part 1 & Part 2
If you can't structure your reports into sub-reports, then there is good & bad news...
Bad news: there's no straightforward way to achieve this in the framework.
Good news: it's possible by using the PDFsharp 3rd-party library. It's open source & free to use, even in commercial applications.
See previous question: Rendering multiple .rdlc reports into a single PDF using PDFSharp
Good luck!

I have dealt with this issue for a few days, but I finally found a solution. Clicking one of the two buttons in a form will load the corresponding report. I hope it works for those interested :)
Solution Name : GelirGiderYonetimi
private void giderBtn_Click(object sender, EventArgs e)
{
giderPnl.Visible = true; gelirPnl.Visible = false;
this.GiderTableAdapter.Fill(this.giderDS.Gider);
ReportDataSource r = new ReportDataSource("DataSet1", giderDS.Tables[0]);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(r);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "GelirGiderYonetimi.giderRapor.rdlc";
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();
}
private void gelirBtn_Click(object sender, EventArgs e)
{
giderPnl.Visible = false; gelirPnl.Visible = true;
this.GelirTableAdapter.Fill(this.gelirDS.Gelir);
ReportDataSource r = new ReportDataSource("DataSet1", gelirDS.Tables[0]);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(r);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "GelirGiderYonetimi.gelirRapor.rdlc";
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();
}

Related

How to export to excel from a report on SSRS via ReportViewer control

Maybe this question was asked already feel free to point me to the right direction if it is.
I have a report on SSRS and my Dataset/Datasource is there of course. The Dataset is a Dynamic Query.
The idea is to export the result of the report via Excel.
I have a web server control ReportViewer in my Aspx. Does anyone knows if there is a method to export to excel and how I can code it in my code behind.
Please let me know if you need more info and I will post it.
EDIT:
Checking the info and the other links I did the following Method.
private void CreateEXCEL(Dictionary<string, string> parametros, string nombreReporte)
{
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var d in parametros)
{
parameters.Add(new ReportParameter(d.Key, d.Value));
}
// Setup the report viewer object and get the array of bytes
MyReportViewer.ProcessingMode = ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportPath = nombreReporte;
MyReportViewer.ServerReport.SetParameters(parameters);
byte[] bytes = MyReportViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + nombreReporte + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download
}
But when I execute my web app and call this method I have the following error:
The Server commited a protocol infraction Section=ResponseStatusLine
Did anyone come accross a problem like this?
You can use .Render method:
LocalReport.Render
ServerReport.Render
The example provided by MSDN is for Excel but you can use the same method for PDF and any other supported formats.
Note that is also possible to manually export to Excel/PDF using the built-in functionality of ReportViewer.
Hi buddy if your columns are dynamic in the dataset then there is no such optio n in SSRS. But I can give you a hint like when you edit and SSRS it would like XML.
Keeping that in mind you can construct the entire structure of the dataset as an RDLC file from your code.
To be specific you need to generate RDLC file during runtime. Use the some common template and replace the template place holders with column value's
Personally I assure you it won't take much time to export the data. But you should put some effort to achieve this.
I'm telling this solution from my past experience. I have done something like this :)
Feel free to ask if you need more information. But don't ask code.
The Server commited a protocol infraction Section=ResponseStatusLine
Here's an answer that might help:
https://stackoverflow.com/a/4289266/3854195

Render SSRS report in PDF format in ASP.NET

I have some deployed SSRS reports on the server. Now I am accessing those reports from my ASP.NET application. After some research I found that default print button of the ReportViewer will not be visible in Chrome or any web browser except Internet Explorer. So all I want to render the Reports in PDF format so user can take the print out of PDF format without saving the file. Here's the piece of code I am using:
IReportServerCredentials irsc = DBConnection.NetworkCredentials();
ReportViewer1.ServerReport.ReportServerCredentials = irsc;
List<ReportParameter> list = new List<ReportParameter>();
list.Add(new ReportParameter("ref_no", refNo));
ReportViewer1.ServerReport.SetParameters(list);
ReportViewer1.ServerReport.Render("PDF");
ReportViewer1.ServerReport.Refresh();
But its not working. The reports are getting rendered but not in PDF format. And if someone take the print out, alignment of all the fields like Tablix, TextBox are not properly aligned. Does any one know how can I do this?
Fortunately, after so many try, have found the solution. This is what I have done:
IReportServerCredentials irsc = DBConnection.NetworkCredentials();
ReportViewer1.ServerReport.ReportServerCredentials = irsc;
List<ReportParameter> list = new List<ReportParameter>();
list.Add(new ReportParameter("ref_no", refNo));
ReportViewer1.ServerReport.SetParameters(list);
byte[] data = ReportViewer1.ServerReport.Render("pdf");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline;filename=Manifest_Print.pdf");
Response.BinaryWrite(data);
And it worked for me.

how I can create a excel file in asp.net out of a listview?

i have two question to asp.net and listview. I have a webapplication with a listview. this listview fill with data from users by active directory. the listview show only searched user.
Question 1:
I want to import this ListView in a Excel file. How I can do this? Must I use CSV for this?
Question 2:
I want print out this listview. How I can do this or is it better if I make a excel file and the user print this file :/
I need tipps ans good links :)
thanks <3
1 - Please check these links about how to export listview to excel file :
http://forums.asp.net/p/1245474/2583840.aspx
http://www.c-sharpcorner.com/uploadfile/vasanthks/export-gridviewlistview-to-excel-with-color-formatting/
http://www.dotnetlogix.com/article/aspnet/56/How-to-export-listview-in-excel-in-asp.net.html
2 - For Printing you can do Reporting :)
Good luck!
If you only want to print out the list, then you can print from the web-interface. Create a page with the content you want to print and the follow these instructions:
http://www.w3schools.com/jsref/met_win_print.asp
Question 1: No, you don't need to use CSV. You could use EPPlus for exporting to Excel. It does an amazing job and there's not much you can't do with it in terms of exporting.
Question 2: I'd use the above, but it depends on the requirements of the person who's printing the file. You could just dump a plain list, but Excel would be better IMHO.
Just to complete this... some pseudo code for you to generate a simple spreadsheet...
using OfficeOpenXml;
using (ExcelPackage outPackage = new ExcelPackage(YOUR_DESTINATION_FILENAME))
{
// Add new worksheet
ExcelWorksheet destWorkSheet = outPackage.Workbook.Worksheets.Add("Spreadsheet name");
// Draw header
destWorkSheet.Cells[1, 1].Value = "Header 1";
destWorkSheet.Cells[1, 2].Value = "Header 2";
// Loop through your data and add rows
for (int i = 0; i < YOURDATA.Count; i++)
{
destWorkSheet.Cells[i+2, 1].Value = YOUR_DATA_1;
destWorkSheet.Cells[i+2, 2].Value = YOUR_DATA_2;
}
// Save spreadsheet
outPackage.Save();
}
Hope this helps,
{
Response.ClearContent();
Response.Buffer = true;
string fileName="filname.xlsm";Response.AddHeader("content-disposition", string.Format("attachment;filename={0}",fileName));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
ListView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
well for printing and saving to Excel , PDF, Word and printing purposes i prefer using RDLC as its easier to maintain it. i don't have good experience using this third party office controls and dlls.

Make A4 reports with background image in C# (like Jasper Reports and iReport)

I just want to put some values from my database on an A4 page (I have a JPG template)
and create a PDF book/report with an insert per page.
I have easily done it with NetBeans Java Jasper Reports using iReport editor.
It seems so much more difficult in Visual Studio C# Crystal Reports.
I've really searched for tutorials for Crystal Reports and none of them
is using an A4 image as a template. Please help me if you know any such tutorials.
I prefer a solution which works programmatically and not through a wizard.
I already manage my database with my program. I just need the report and some
documentation of how to give input values to the report. I don't even
need for the report to access the database. I can get all the values in my
program. The best solution for me is a template with my JPG file as background
and boxes (like textboxes) where I give text from my program through parameters
of a function. Like Jasper Reports / iReport.
OK, I spend some time to find an easy solution and I got the following:
First of all I didn't use Crystal Reports, but Windows Reports (rdlc files).
Windows Reports are more simple and easy and it is possible to add an image as a background
and above this image some TextBoxes that refer to String parameters (exactly what I needed). They are in Visual Studio by default and you design your report in Visual Studio (right click in Solution Explorer --> Add Report)
Then I found a code sample which converts the report to PDF files and I used it to write the following Class:
public class XReport
{
private ReportViewer reportViewer = new ReportViewer();
public XReport()
{
}
public XReport(String reportFilePath)
{
setReportFile(reportFilePath);
}
// set rdlc file
public void setReportFile(String reportFilePath)
{
reportViewer.Reset();
reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer.LocalReport.ReportPath = reportFilePath;
}
public void setParameters(List<ReportParameter> parameters)
{
reportViewer.LocalReport.SetParameters(parameters.ToArray());
reportViewer.LocalReport.Refresh();
}
public void setParameters(Dictionary<String, String> parameters)
{
XList<ReportParameter> parameterList = new List<ReportParameter>();
XList<String> parameterKeys = parameters.getKeys();
foreach (String parameterKey in parameterKeys) {
parameterList.Add(new ReportParameter(parameterKey, parameters.get(parameterKey))); }
setParameters(parameterList);
}
public void exportToPDF(String pdfFilePath)
{
Warning[] warnings;
string[] streamids;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = null;
// render PDF file
try { bytes = reportViewer.LocalReport.Render( "PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); }
catch (Exception ex) { System.Console.Write(ex.Message); }
// write PDF file
using (FileStream fs = new FileStream(pdfFilePath, FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); fs.Close(); }
// release reportViewer resources to avoid errors
reportViewer.LocalReport.ReleaseSandboxAppDomain();
}
}
It works. Try it. Be careful with 2 things:
Use correct paths for reportFilePath and pdfFilePath. (pdfFilePath worked only with non-relative path for me)
Make sure that you have added all parameters with their correct names in your rdlc report. You can add them at View --> Report Data --> Add New Parameter (right click at Parameters). Also see this: Create a parameter in rdlc report
Hope I helped. It worked for me great.
You can set the paper size of the report using the below mentioned option in Crystal itself.
Open the report first and then go to
File -> Page Setup

Excel Report Setting page setup options in C#

I am using the following approach to create a dynamic Excel Report from HTML in my ASP.NET Application.
While creating the excel report, I want to set the Page Setup options such that the report fits into one page.
This is the approach I'm using to create the excel report...
Please tell me how to fit this report to one page or to zoom in the report?
string sContentType = "application/vnd.excel";
string sFileName = "Detail Qutation.xls";
Hashtable oData = new Hashtable();
oData.Add("Content-Disposition", "attachment;filename=" + sFileName);
oData.Add("Charset", "");
oData.Add("ContentType", sContentType);
oData.Add("Content", sTable.ToString());
oData.Add("IsBinaryContent", false);
this.StoreObject("Document", oData);
You might need to render using htmltextwriter as explained in
http://forums.asp.net/p/1063850/1538066.aspx#1538066
and take the concept to get a solution.

Categories

Resources