Render SSRS report in PDF format in ASP.NET - c#

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.

Related

Render multiple reports in one report viewer (rdlc)

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();
}

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

Response image src in asp.net mvc

I return HTML content and display in excel file in ASP.NET MVC.
My question is about img source.Img source does not work.
My Code:
public ActionResult ExportData()
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
var stringWrite = new StringWriter();
var htmlWrite = new HtmlTextWriter(stringWrite);
var headerTable = #"<Table><tr><td><img src=""‪C:\Users\xx\Desktop\Export\Export\Images\orderedList0.png"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());
Response.End();
return RedirectToAction("StudentDetails");
}
Above code exports excell file without problem. I only have problem about displaying image. Image can not appear exactly. There is a problem about img source.
What is the correct way for img src for my code?
Please keep in mind the path to the image you are embedding into Excel is an absolute path which MUST EXIST on the client's machine at that exact path.
I assume you get an image placeholder within the workbook? If so, it's a path problem. You will have to make sure the path points correctly to a file which exists at that exact path on that exact machine.
A much better idea would be to alter the src tag to be relative as Ash suggested and host the image from your webserver
var headerTable = #"<Table><tr><td><img src=""fullyqualifiedurltoimagefileonserver.png"" \></td></tr></Table>";
If you are 100% certain on the image path you may be facing an Excel security limitation.

How to print RDLC Report in Firefox

I want to print my RDLC report in Firefox but the Print button icon is not displaying in Firefox browser but It works fine in IE.
Can anyone guide me how to print my report in Firefox browser.
Unfortunately there is no way to print an rdlc report in firefox.
Rdlc print is only works in ie, due to the activeX requirements.
More info here:Configuring and Using the ReportViewer Toolbar
There is an article on msdn about how print a local report without preview, but its only works with winforms report viewer. In asp.net you cannot acces to the client's printer.
Walkthrough: Printing a Local Report without Preview
Instead of print in firefox, you can export rdlc directly.
For an example:
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
string filename = "YourFileName";
// Setup the report viewer object and get the array of bytes
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "YourReportHere.rdlc";
byte[] bytes = viewer.LocalReport.Render("PDF", 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=" + fileName + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download

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