RDLC 用ashx调用打印的例子

来源:互联网 发布:淘宝最近生意不好 编辑:程序博客网 时间:2024/05/21 06:54

研究了好久,开始是嵌入aspx页面中导出的打印的,觉得这样太浪费资源了所以现在嵌入到比较不耗资源的ashx文件中,其中涉及了RDLC的导出、嵌入aspx与ashx的不同点:

以下即ashx代码:

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("DataSet1", dt);   //必须指定“DataSet1”为数据集中名称



            localReport.DataSources.Add(reportDataSource);

            string reportType = "PDF";         //可以换成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;
            }
        }
    }
}



原创粉丝点击