水晶报表的打印

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;
        }
    }
}
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值