研究了好久,開始是嵌入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;
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 should 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();
}
}
}