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文件的类名必须和访问数据库返回的表名必须相同,否则会报登录数据库失败的错误 见了他,她变得很低很低,低到尘埃里。但她心里是欢喜的,从尘埃里开出花来。魂牵梦绕,辗转反侧,可越是这样执着的爱,越是让我束手无策,无法表达,成就一段烽火佳话,迎接她包含深情炙热的眼光吧
|
请发表评论