在咱們開發各類工具軟件的時候,咱們不可避免的會遇到打印的問題。而使用.NET開發打印功能,水晶報表或許會是一個十分不象錯的選擇!html
通常步驟:數據庫
水晶報表的兩種模式:app
相關控件:框架
命名空間:工具
相關成員:post
類名 | 成員名 | 描述 |
CrystalReport | Load | 加載水晶報表(.rpt)文件 |
SetDatabaseLogon | 設置數據庫鏈接,PULL中會用到 | |
SetParameterValue | 設置報表值 | |
CrystalReportViewer | ReportSource | 設置報表數據源 |
DataBind | 綁定數據源 | |
CrystalReportSource | ReportDocument.Load | 加載水晶報表(.rpt)文件,Server.MapPath("*********.rpt") |
ReportDocument.SetDatabaseLogon | 設置數據庫鏈接,PULL中會用到 | |
ReportDocument.SetParameterValue | 設置報表值 | |
打印模板文件樣式spa
Form2樣式code
Form1orm
using System.Windows; // 引入能夠訪問 app.config 中的項 using System.Drawing.Printing; // 打印必須 namespace csdemo.reportdemo { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow: Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 獲取可用打印機 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, RoutedEventArgs e) { comboBox1.Items.Clear(); foreach(var item in PrinterSettings.InstalledPrinters) { comboBox1.Items.Add(item.ToString()); } comboBox1.SelectedIndex = 0; } /// <summary> /// 打印機名稱 /// </summary> private string _printerName = string.Empty; /// <summary> /// 設置打印機 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, RoutedEventArgs e) { _printerName = comboBox1.SelectedValue.ToString(); } /// <summary> /// 調用打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, RoutedEventArgs e) { PrintWindow pw = new PrintWindow(); pw.printerName = _printerName; pw.Show(); pw.Close(); } } }
Form2htm
using System.Windows; using System.Data.SqlClient; using CrystalDecisions.CrystalReports.Engine; namespace csdemo.reportdemo { /// <summary> /// PrintWindow.xaml 的交互邏輯 /// </summary> public partial class PrintWindow: Window { public PrintWindow() { InitializeComponent(); Data_Binding(); } public string printerName = string.Empty; private void Data_Binding() { System.Data.DataSet ds = new System.Data.DataSet(); SqlConnection conn = new SqlConnection(); conn.ConnectionString = @ "Data Source=192.168.0.196;Initial Catalog=NewEMaxTest;Persist Security Info=True;User ID=sa;Password=qwerta"; conn.Open(); string cmd = "select * from TBusRetail"; SqlDataAdapter da = new SqlDataAdapter(cmd, conn); da.Fill(ds, "TBusRetail"); conn.Close(); string pathRpt = @ "D:\Projects\csdemo\branches\csdemo2010\csdemo.reportdemo\ReportFile\DemoCrystalReport.rpt"; // **************************************************** ReportDocument repostDoc = new ReportDocument(); repostDoc.Load(pathRpt); // 加載打印模板文件 repostDoc.SetDataSource(ds); // 設置數據源 repostDoc.PrintOptions.PrinterName = printerName; // 設置打印機名稱 repostDoc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4; // 設置打印紙張樣式 repostDoc.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.DefaultPaperOrientation; repostDoc.PrintToPrinter(1, false, 1, 1); // 只打印一頁,不覈對,從第 0 頁打印到第0頁 // **************************************************** // 若是要顯示數據的話,就可使用這個來在Viwer中綁定數據源了。 // crv.ViewerCore.ReportSource = repostDoc; } } }
注意:
參考網摘: