程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 水晶報表的打印

水晶報表的打印

編輯:.NET實例教程
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved