In this article, I want to show a method about exporting data table in website to PDF with ASP.NET.
The PDF template is .NET report file (.rdlc). In this method, I fill data to report files. And then export PDF byte stream by using method of Microsoft.Reporting.WebForms.LocalReport class. Finally, define HTTP header through defining Response to export
PDF file.
Code for Important Part:
1. At first, prepare a method to export PDF byte stream.
using System;
using System.Collections.Generic;
using ATA.Toeic.Models;
using ATA.Toeic.DAL;
using System.IO;
using Microsoft.Reporting.WebForms;
namespace ATA.Toeic.BLL
{
public class AdmissionTicketBLL
{
private RegistrationDAL dal = new RegistrationDAL();
/// <summary>
/// Export Singly
/// Export a PDF File Including one Admission Ticket
/// </summary>
/// <param name="addmissionFormId">ticket number</param>
/// <param name="reportPath">report template path</param>
/// <returns>PDF file byte stream</returns>
public byte[] ExportTicket(string addmissionFormId, string reportPath, out string mimeType)
{
List<string> arrId = new List<string>();
arrId.Add(addmissionFormId);
return ExportTicket(arrId, reportPath, out mimeType);
}
/// <summary>
/// Export Bath
/// Export PDF File Including Several Admission Tickets
/// </summary>
/// <param name="arrAddmissionFormId">ticket number needed to be exported</param>
/// <returns>PDF file byte stream</returns>
public byte[] ExportTicket(List<string> arrAddmissionFormId, string reportPath, out string mimeType)
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = reportPath;
// Template File of Report Object Path
// Data Source of Report Object
ReportDataSource reportDataSource = new ReportDataSource("dsList", GetAdmissionTicketList(arrAddmissionFormId.ToArray())
// Return IList<Model> Object );
localReport.DataSources.Add(reportDataSource);
string reportType = "PDF";
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat></DeviceInfo>"; Warning[] warnings; string[] streams; byte[] renderedBytes; //Render the report
renderedBytes = localReport.Render( reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
return renderedBytes;
}
}
2. Define HTTP header in action. And then export PDF file. Now, there is no difference between batch and single. The differences has been included in byte stream of byte[].
[CheckServiceExpire]
public ActionResult GetAdmissionForms(int serviceid, string Condition)
{
List<RegistrationEn> list = new RegistrationBLL().GetExamineeByCondi(serviceid, Condition);
if (list == null || list.Count == 0)
return Alert("No Ticket Information", "~/Views/ExamService/SaveSuccess.aspx", new { action = "GetExamineeByPage", controller = "Registration", serviceid = serviceid });
List<string> sl = new List<string>();
foreach (RegistrationEn ren in list)
sl.Add(ren.fAdmissionFormId);
try
{
AdmissionTicketBLL bll = new AdmissionTicketBLL();
string rdlcPath = Server.MapPath("~/Resources/AdmissionTicket.rdlc");
string mimeType;
byte[] renderedBytes = bll.ExportTicket(sl, rdlcPath, out mimeType);
Response.AddHeader("content-disposition", "attachment; filename=AdmissionTicket.pdf");
return File(renderedBytes, mimeType);
} catch {
return Alert("Error to Get Ticket Inforamtion", "~/Views/ExamService/SaveSuccess.aspx", new { action = "GetExamineeByPage", controller = "Registration", serviceid = serviceid });
}
}
Then, we can get the wanted PDF file.
Addition:
If we want to open PDF directly in website (Adobe Reader must be installed), we just need to modify parameter of HTTP header.
Replace Response.AddHeader("content-disposition", "attachment; filename=AdmissionTicket.pdf"); to Response.AddHeader("content-disposition", string.Format("inline;filename={0}.pdf", admissionFormId));
None
0 Points
3 Posts
Export Data to PDF from MVC with ASP.NET
Aug 08, 2011 05:15 AM|S.Green|LINK
Introduction:
In this article, I want to show a method about exporting data table in website to PDF with ASP.NET.
The PDF template is .NET report file (.rdlc). In this method, I fill data to report files. And then export PDF byte stream by using method of Microsoft.Reporting.WebForms.LocalReport class. Finally, define HTTP header through defining Response to export PDF file.
Code for Important Part:
1. At first, prepare a method to export PDF byte stream.
2. Define HTTP header in action. And then export PDF file. Now, there is no difference between batch and single. The differences has been included in byte stream of byte[].
Then, we can get the wanted PDF file.
Addition:
If we want to open PDF directly in website (Adobe Reader must be installed), we just need to modify parameter of HTTP header.
Finally, I want to introduce an article about exporting data with C#, http://www.codeproject.com/KB/cs/Excel_PDF_Word_ExportWiz.aspx.
It shows a Data Export Wizard and code for how to create this wizard.