当前位置:编程学习 > C#/ASP.NET >>

RDLC 用ashx调用打印的例子

研究了好久,开始是嵌入aspx页面中导出的打印的,觉得这样太浪费资源了所以现在嵌入到比较不耗资源的ashx文件中,其中涉及了RDLC的导出、嵌入aspx与ashx的不同点:
 
以下即ashx代码:
 
[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>  
    /// 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_Docnum_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);   //必须指定“DataSet1”为数据集中名称  
  
  
  
            localReport.DataSources.Add(reportDataSource);  
  
            string reportType = "<span style="color:#FF0000;">PDF</span>";         //可以换成EXCEL,完成EXCEL导出功能。  
            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;  
            //生成PDF文件到renderedBytes中  
            renderedBytes = localReport.Render(  
                reportType,  
                deviceInfo,  
                out mimeType,  
                out encoding,  
                out fileNameExtension,   //可以在此设置文件名  
                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;  
            }  
        }  
    }  
}  
 
 
备注:1、HttpContext.Current.Server.MapPath("");  在项目中要引入"microsoft.reportview.webforms";
 
            2、 ReportDataSource("DataSet1", dt)  中DataSet1名字必须与RDLC中命名的文件名相同;(如下可能好理解点)
 
 
另外附上ASPX中的导出代码供对比用:
 
[csharp]  
using System;  
using System.Collections.Generic;  
using System.Web;  
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,