using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Text; using System.Windows.Forms; namespace GSBase { public partial class MyFastReportMain : Form { public MyFastReportMain() { InitializeComponent(); } private DataSet Tables = null; public FastReport.Report MyReport = new FastReport.Report(); private PrinterSettings printerSettings=null ; private PrintType PrintType; /// /// 外部调用(预览模式和打印模式) /// /// /// /// 报表文件模板 public void Print(PrintType _printType, DataSet _ds, string _reportstring) { Tables = _ds; PrintType = _printType; DoPrint(_reportstring); } public void PrintSettings(PrinterSettings A) { printerSettings = A; } /// /// 内部调用 /// /// private void DoPrint(string reportstring) { MyReport.LoadFromString(reportstring); DataSet ds; if (Tables == null || Tables.Tables.Count == 0) { ds = new DataSet("MyTables"); DataTable dt = new DataTable("MyTableOnew"); dt.Columns.Add("name"); dt.Columns.Add("age"); dt.Columns.Add("sex"); for (int i = 1; i <= 100; i++) { DataRow dr = dt.NewRow(); dr["name"] = "小明" + i.ToString(); dr["age"] = i.ToString(); dr["sex"] = "男"; dt.Rows.Add(dr); } ds.Tables.Add(dt); } else { ds = Tables; } MyReport.RegisterData(ds); MyReport.Preview = this.previewControl1; switch (PrintType) { case PrintType.DesignReport: MyReport.Design(); break; case PrintType.PreviewReport: printerSettings = null;//预览时清除打印机,让用户有选择打印机的机会 MyReport.Show(); this.ShowDialog(); break; case PrintType.PrintReport: try { if (MyReport.Prepare() == false) return; //如果打印设备为空,则让用户选择打印机。 if (printerSettings == null) { if (MyReport.ShowPrintDialog(out printerSettings) == false) return; } MyReport.PrintPrepared(printerSettings); // MyReport.PrintSettings.ShowDialog = false; } catch (Exception e) { MessageBox.Show("打印报表出错,信息如下:" + e.Message); printerSettings = null; return; } break; default: MessageBox.Show("没有提供此类操作!"); return; } } } /// /// 报表的打印类型 /// public enum PrintType { /// /// 设计报表 /// DesignReport = 1, /// /// 预览报表 /// PreviewReport, /// /// 打印报表 /// PrintReport, /// /// 创建新表表 /// CreateNewReport }; }