啊鑫
2024-07-09 0552fcc8cb73fc3021e2915129f55a42ed3f20e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
 
    /// <summary>
    /// 外部调用(预览模式和打印模式)
    /// </summary>
    /// <param name="_printType"></param>
    /// <param name="_ds"></param>
    /// <param name="_reportstring">报表文件模板</param>
    public void Print(PrintType _printType, DataSet _ds, string _reportstring)
    {
        Tables = _ds;
        PrintType = _printType;
        DoPrint(_reportstring);
    }
 
        public void PrintSettings(PrinterSettings A)
        {
            printerSettings = A;
 
        }
 
        /// <summary>
        /// 内部调用
        /// </summary>
        /// <param name="reportstring"></param>
        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;
        }
    }
}
 
/// <summary>
///  报表的打印类型
/// </summary>
public enum PrintType
{
    /// <summary>
    /// 设计报表
    /// </summary>
    DesignReport = 1,
 
    /// <summary>
    /// 预览报表
    /// </summary>
    PreviewReport,
 
    /// <summary>
    /// 打印报表
    /// </summary>
    PrintReport,
 
    /// <summary>
    /// 创建新表表
    /// </summary>
    CreateNewReport
};
}