Trong bài viết dưới đây tác giả hướng dẫn chúng ta làm việc đó. Kỹ thuật ở đây là ta phải thêm 1 button và xử lý code cho nó để xuất ra PDF hoặc Excel, đoạn code cũng không quá rắc rối để chúng ta tìm hiểu.
------------------------------------------------------------------------------------------------------------
Export SSRS Report into PDF Format from ASP.NET Application
Now a days, it is often required to execute SSRS Report from ASP.NET web application and export that report on the disk in different formats like PDF,XLS etc.
In this article, I will show you how to Export SSRS Report into PDF from ASP.NET Web Application.
I assumed that you already developed a SSRS Report and deployed it to the Report Server.
1. First of all open Visual Studio 2010 and create a new website or open an existing website. In that add new web page and give appropriate name.
2. Now in your web page add following controls:
- One ScriptManager
- One Button
- One MicrosoftReportViewer

3. Now add following code on button’s click event in your .cs file :
MyReportViewer.ProcessingMode = ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportServerUrl =
new
Uri(
"http://10.88.141.76:8080/ReportServer2012"
); // Report Server URL
MyReportViewer.ServerReport.ReportPath =
"/StartSSRS/PersonAddressDetails"
;
// Report Name
MyReportViewer.ServerReport.Refresh();
Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection =
new
Microsoft.Reporting.WebForms.ReportParameter[1];
reportParameterCollection[0] =
new
Microsoft.Reporting.WebForms.ReportParameter();
reportParameterCollection[0].Name =
"City"
;
//Parameter Name
reportParameterCollection[0].Values.Add(
"Seattle"
);
//Parameter Value
MyReportViewer.ServerReport.SetParameters(reportParameterCollection);
Warning[] warnings;
string
[] streamids;
string
mimeType, encoding, extension, deviceInfo;
deviceInfo =
"True"
;
byte
[] bytes = MyReportViewer.ServerReport.Render(
"PDF"
,
null
,
out
mimeType,
out
encoding,
out
extension,
out
streamids,
out
warnings);
Response.Buffer =
true
;
Response.Clear();
Response.ContentType = mimeType;
/*
This header is for saving it as an Attachment and popup window should display to to offer save as or open a PDF file
Response.AddHeader("Content-Disposition", "attachment; filename=" + extension);
*/
/*
This header is use for open it in browser.
Response.AddHeader("content-disposition", "inline; filename=myfile." + extension);
Response.BinaryWrite(bytes);
*/
//Creatr PDF file on disk
string
pdfPath =
@"D:\TempReports\PersonAddressDetails."
+ extension;
// Path to export Report.
System.IO.FileStream pdfFile =
new
System.IO.FileStream(pdfPath, System.IO.FileMode.Create);
pdfFile.Write(bytes, 0, bytes.Length);
pdfFile.Close();
Response.Flush();
Response.End();
4. Now set this page as a startup and then press F5. In browser click on Export Report button. It will export report on the disk.
Now browse to path on which Report is Exported. You will see report over there.

Similarly, If you want to export report in excel, make following changes in line no18 of above code:
byte
[] bytes = MyReportViewer.ServerReport.Render(
"EXCEL"
,
null
,
out
mimeType,
out
encoding,
out
extension,
out
streamids,
out
warnings);
Tham khảo:
http://bhushan.extreme-advice.com/export-ssrs-report-into-pdf-format-from-asp-net-application/
0 comments:
Post a Comment