Example of printing by calling ashx in RDLC

Source: Internet
Author: User

After studying for a long time, I started to embed the exported print on the aspx page. I think this is a waste of resources, so now I am embedded in the ashx file that is not consuming resources, the differences between exporting and embedding aspx and ashx of RDLC are involved: the following is the ashx code: [csharp] using System; using System. collections. generic; using System. web; using Microsoft. reporting. webForms; using System. data; using System. data. sqlClient; namespace V3WEB. ashx. car {// <summary> /// abstract of Tra_Docnum_CostCount_Rpt /// </summary> public class Tra_Docnum_CostCount_Rpt: IHttpHandler {Public void ProcessRequest (HttpContext context) {ExportPDF (context);} private DataTable LoadData (HttpContext context) {DataTable dt = null; string d1 = "2012-01-01 "; // Request ["d1"]. toString (); string d2 = "2012-01-01"; // Request ["d2"]. toString (); string dept = ""; // Request ["dept"]. toString (); string carno = ""; // Request ["carno"]. toString (); dt = BLL. car. factory. getTra_DocnumBLL (). tra_Do Cnum_CostCount (d1, d2, dept, carno); return dt;} public void ExportPDF (HttpContext context) {LocalReport localReport = new LocalReport (); localReport. reportPath = HttpContext. current. server. mapPath ("/Report/Car/Tra_Docnum_CostCount.rdlc"); DataTable dt = LoadData (context); ReportDataSource reportDataSource = new ReportDataSource ("<span style =" color: # FF0000; "> DataSet1 </span>", dt); // you must specify 1 "is the name of the dataset localReport. dataSources. add (reportDataSource); string reportType = "<span style =" color: # FF0000; "> PDF </span>"; // EXCEL can be used to export data. String mimeType; string encoding; string fileNameExtension; string deviceInfo = "<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> "; warning [] warnings; string [] streams; byte [] renderedBytes; // generate a PDF file to renderedBytes, renderedBytes = localReport. render (reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, // you can set the file name out streams, out warnings); context. response. buffer = true; context. response. clear (); context. response. contentType = mimeType; context. response. binaryWrite (renderedBytes); context. response. flush (); context. response. end () ;}public bool IsReusable {get {return false ;}}}note: 1. HttpContext. current. server. mapPath (""); Introduce "microsoft. reportview. webforms "; 2. In ReportDataSource (" DataSet1 ", dt), the name of DataSet1 must be the same as the name of the file named in RDLC. (the following may be a good understanding) in addition, the export code in ASPX is attached for comparison: [csharp] using System; using System. collections. generic; using System. web; using System. web. UI; using System. web. UI. webControls; using Microsoft. reporting. webForms; using System. data; using System. data. sqlClient; namespace V3WEB. report. car {public partial class Tra_Docnum_CostCount_Rpt: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {ExportPDF ();} private DataTable LoadData () {DataTable dt = null; string d1 = "2012-01-01 "; // Request ["d1"]. toString (); string d2 = "2012-01-01"; // Request ["d2"]. toString (); string dept = ""; // Request ["dept"]. toString (); string carno = ""; // Request ["carno"]. toString (); dt = BLL. car. factory. getTra_DocnumBLL (). tra_Docnum_CostCount (d1, d2, dept, carno); return dt;} public void ExportPDF () {LocalReport localReport = new LocalReport (); localReport. reportPath = Server. mapPath ("Tra_Docnum_CostCount.rdlc"); DataTable dt = LoadData (); ReportDataSource reportDataSource = new ReportDataSource ("DataSet1", dt); localReport. dataSources. add (reportDataSource); string reportType = "PDF"; string mimeType; string encoding; string fileNameExtension; // The DeviceInfo settings shocould be changed based on the reportType // http://msdn2.microsoft.com/en-us/library/ms155397.aspx string deviceInfo = "<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> "; warning [] warnings; string [] streams; byte [] renderedBytes; // Render the report renderedBytes = localReport. render (reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); Response. buffer = true; Response. clear (); Response. contentType = mimeType; Response. binaryWrite (renderedBytes); Response. flush (); Response. end ();}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.