现在的位置: 首页 > 综合 > 正文

水晶报表的打印

2013年03月12日 ⁄ 综合 ⁄ 共 4294字 ⁄ 字号 评论关闭
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.Sql;
using System.Data.SqlClient ;
public partial class print : System.Web.UI.UserControl
{
 
    //纸张资源列表
    private ArrayList GetPaperSources()
    {
        ArrayList arrayList = new ArrayList();
        System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
        printerSettings.PrinterName = this.thePrinter.SelectedValue;
        foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
        {
            arrayList.Add(paperSource.SourceName.ToString());
        }
        return arrayList;
    }
    //获得打印机列表
    private ArrayList GetPrinter()
    {
        ArrayList array = new ArrayList();
        foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            array.Add(iprt);
        return array;
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
      
         
        if (!IsPostBack)
        {
            initControl();
            loadReport();
        }
        paperSourceList.DataSource = GetPaperSources();
        DataBind();
 
    }
    //设置打印选项
    private void SetPrintOptions()
    {
        PrintOptions printOptions = this.CrystalReportSource1.ReportDocument.PrintOptions;
        printOptions.PrinterName = this.thePrinter.SelectedValue;
        printOptions.PaperOrientation = (PaperOrientation)paperOrientationList.SelectedIndex;
        printOptions.PaperSize = (PaperSize)paperSizeList.SelectedIndex;
        printOptions.PrinterDuplex = (PrinterDuplex)printerDuplexList.SelectedIndex;
        printOptions.CustomPaperSource = GetSelectedPaperSource();
    }
    //实例化下拉列表
    private void initControl()
    {
        paperOrientationList.DataSource = System.Enum.GetValues(typeof(PaperOrientation));
        paperSizeList.DataSource = System.Enum.GetValues(typeof(PaperSize));
        printerDuplexList.DataSource = System.Enum.GetValues(typeof(PrinterDuplex));
       
       
        thePrinter.DataSource = GetPrinter();
        paperSourceList.DataSource = GetPaperSources();
        DataBind();
    }
    //获得选定的打印机资源
    private System.Drawing.Printing.PaperSource GetSelectedPaperSource()
    {
        System.Drawing.Printing.PaperSource selectedPaperSource = new System.Drawing.Printing.PaperSource();
        System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
        printerSettings.PrinterName = this.thePrinter.SelectedValue;
        foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
        {
            if (paperSource.SourceName == paperSourceList.SelectedItem.Text)
            {
                selectedPaperSource = paperSource;
            }
        }
        return selectedPaperSource;
    }
    // 加载报表
   
    private void loadReport()
    {
        try
        {
            string sql = "Select * from Users";
     
            DataSet ds = new DataSet();
            //ds = new DataSet();
            SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["DB301ConnectionString"].ConnectionString);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand = sqlCmd;
            sqlAd.Fill(ds, "Users");
 
            CrystalReportSource1.ReportDocument.Load(Server.MapPath("printUser.rpt"));
            //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
            CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["Users"]);
            //{?}中的参数可以不用赋值,即使赋了值也不起作用。
      
            CrystalReportSource1.DataBind();
            CrystalReportViewer1.ReportSource = CrystalReportSource1;
            CrystalReportViewer1.DataBind();
            //hierarchicalGroupingReport = this.CrystalReportSource1.ReportDocument;
        }
        catch (Exception exc)
        {
            Response.Write(exc.Message);
        }
    }
    //打印
    protected void printReport_Click(object sender, EventArgs e)
    {
       
        SetPrintOptions();
        try
        {
         
            this.CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 1, 99);
            message.Text = "success";
        }
        catch (Exception aaa)
        {
            message.Text = aaa.Message;
        }
    }
}

 

抱歉!评论已关闭.