C#實現導出pdf文件,打印
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessFacade;
using System.IO;
using Common.Table;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
public partial class Pages_ReportFile_BhXjspf_XjspfCheckList : PageBase
{
ReportDocument myReportDoc = new ReportDocument();
private void SetParameterValue(string strColumn, string strValue)
{
myReportDoc.SetParameterValue(strColumn, strValue);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strResult0 = "";
string strResult1 = "";
string strResult2 = "";
string strBuildArea = string.Empty;
string strHouseCount = string.Empty;
string strPerBuild = string.Empty;
string strPage = string.Empty;
string strCount = string.Empty;
this.CheckPageParameter();
string strError = string.Empty;
PrintSystem mySystem = new PrintSystem(base.UserSessionID);
DataSet myData = new DataSet();
myData = mySystem.mothed(this.GetSessionParameterValue("FILE_ID"), out strError);
if (!string.IsNullOrEmpty(strError))
{
webTools.InsertAlert(this.Page, webTools.FilterInvalidString(strError));
webTools.closewin(this.Page);
return;
}
myData.Tables[LY_ZG_XJSPF_TABLE.LY_ZG_XJSPF_TABLENAME].Columns.Add("QRCODE", typeof(System.Byte[]));
myData.Tables[LY_ZG_XJSPF_TABLE.LY_ZG_XJSPF_TABLENAME].Rows[0]["QRCODE"] = ProductQR(myData);
myData.AcceptChanges();
if (!string.IsNullOrEmpty(myData.Tables[0].Rows[0]["GZ_RESULT"].ToString()))
{
switch (myData.Tables[0].Rows[0]["GZ_RESULT"].ToString())
{
case "0":
strResult0 = "√";
strResult1 = "×";
strResult2 = "×";
break;
case "1":
strResult1 = "√";
strResult0 = "×";
strResult2 = "×";
break;
case "2":
strResult2 = "√";
strResult0 = "×";
strResult1 = "×";
break;
}
}
for (int i = 0; i < myData.Tables[LY_FAMILYHOUSEINFORMATION_TABLE.LY_FAMILYHOUSEINFORMATION_TABLENAME].Rows.Count; i++)
{
strHouseCount = myData.Tables[2].Rows[i]["HouseCount"].ToString();
if (!string.IsNullOrEmpty(myData.Tables[2].Rows[i]["SumIDENTIFIEDAREA"].ToString()))
{
strPerBuild = Convert.ToString(Math.Round((Convert.ToDouble(myData.Tables[2].Rows[i]["SumIDENTIFIEDAREA"].ToString()) / Convert.ToDouble(myData.Tables[0].Rows[0]["PersonCount"].ToString())), 2));
strBuildArea = myData.Tables[2].Rows[i]["SumIDENTIFIEDAREA"].ToString();
}
}
for (int i = 0; i < myData.Tables[LY_H_RECEIVE_TABLE.LY_H_RECEIVE_TABLENAME].Rows.Count; i++)
{
strCount = myData.Tables[3].Rows[i]["counts"].ToString();
strPage = myData.Tables[3].Rows[i]["pages"].ToString();
}
myReportDoc.Load(Server.MapPath(@"rptxjspfChecklist.rpt"));
myReportDoc.SetDataSource(myData);//這里必須先設置數據源,然后再設置參數,否則報‘找不到參數’的錯誤
myReportDoc.SetParameterValue("Result0", strResult0);
myReportDoc.SetParameterValue("Result1", strResult1);
myReportDoc.SetParameterValue("Result2", strResult2);
myReportDoc.SetParameterValue("BuildArea", strBuildArea);
myReportDoc.SetParameterValue("HouseCount", strHouseCount);
myReportDoc.SetParameterValue("PerBuild", strPerBuild);
myReportDoc.SetParameterValue("page", strPage);
myReportDoc.SetParameterValue("count", strCount);
try
{
DiskFileDestinationOptions myDestinationFile = new DiskFileDestinationOptions();
string filename = "";
filename = System.DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".pdf";
Stream stream = myReportDoc.ExportToStream(ExportFormatType.PortableDocFormat);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Close();
String contentType = "application/octet-stream";
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.Charset = "GB2312";
Response.ContentType = contentType;
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
catch (System.Exception exp)
{
throw exp;
}
}
}
private byte[] ProductQR(DataSet myData)
{
DataTable mytable = myData.Tables[0];
string strFILE_ID = mytable.Rows[0][LY_ZG_XJSPF_TABLE.FILE_ID_FIELD].ToString().Trim();
string strRegister_num = mytable.Rows[0][LY_ZG_XJSPF_TABLE.REGISTER_NUM_FIELD].ToString().Trim();
#region 設置QR碼
string strCode = strRegister_num + "\r\n";//"審核表編號:" +
strCode += strFILE_ID + "\r\n";//"收件號:" +
QRCode.Service1 QrCode = new QRCode.Service1();
return QrCode.GetQrCode(strCode);
#endregion
}
private void Page_UnLoad(object sender, System.EventArgs e)
{
if (myReportDoc != null)
{
myReportDoc.Close();
myReportDoc.Dispose();
}
}
}
1.注意rpt中的數據庫專家中的xsd文件的類名必須和訪問數據庫返回的表名必須相同,否則會報登錄數據庫失敗的錯誤
見了他,她變得很低很低,低到塵埃里。但她心里是歡喜的,從塵埃里開出花來。魂牽夢繞,輾轉反側,可越是這樣執着的愛,越是讓我束手無策,無法表達,成就一段烽火佳話,迎接她包含深情炙熱的眼光吧