使用XtraGrid打印各种报表,要求能显示表头、条件,设置纸张类型等。
解决办法:
1
using System;
2
using System.Data;
3
using DevExpress.XtraPrintingLinks;
4
using DevExpress.XtraPrinting;
5
using System.Drawing;
6
using System.Drawing.Printing;
7
8
9
namespace Dhthx.Report
10
{
11
/// <summary>
12
/// ReportCenter 的摘要说明。
13
/// </summary>
14
public class ReportCenter
15
{
16
17
private PrintingSystem printSystem;
18
private string mReportName;
19
private string mCondition;
20
21
public ReportCenter(IPrintable Printable)
22
{
23
printSystem = new PrintingSystem();
24
mReportName = "";
25
mCondition = "";
26
PrintableComponentLink pcl = new PrintableComponentLink();
27
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
28
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
29
pcl.Component = Printable;
30
printSystem.Links.Add(pcl);
31
pcl.CreateDocument();
32
33
PrinterSettingsUsing pst = new PrinterSettingsUsing();
34
pst.UseMargins = false;
35
pst.UsePaperKind = false;
36
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
37
printSystem.PageSettings.PaperName = "A4";
38
printSystem.PageSettings.LeftMargin = 2;
39
printSystem.PageSettings.RightMargin = 2;
40
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
41
}
42
public ReportCenter(IPrintable Printable, string ReportName)
43
{
44
printSystem = new PrintingSystem();
45
mReportName = ReportName;
46
mCondition = "";
47
PrintableComponentLink pcl = new PrintableComponentLink();
48
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
49
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
50
pcl.Component = Printable;
51
printSystem.Links.Add(pcl);
52
pcl.CreateDocument();
53
54
PrinterSettingsUsing pst = new PrinterSettingsUsing();
55
pst.UseMargins = false;
56
pst.UsePaperKind = false;
57
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
58
printSystem.PageSettings.PaperName = "A4";
59
printSystem.PageSettings.LeftMargin = 2;
60
printSystem.PageSettings.RightMargin = 2;
61
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
62
}
63
public ReportCenter(IPrintable Printable, string ReportName, string Condition)
64
{
65
printSystem = new PrintingSystem();
66
mReportName = ReportName;
67
mCondition = Condition;
68
PrintableComponentLink pcl = new PrintableComponentLink();
69
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
70
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
71
pcl.Component = Printable;
72
printSystem.Links.Add(pcl);
73
pcl.CreateDocument();
74
75
PrinterSettingsUsing pst = new PrinterSettingsUsing();
76
pst.UseMargins = true;
77
//pst.UsePaperKind = false;
78
printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
79
printSystem.PageSettings.PaperName = "A4";
80
printSystem.PageSettings.LeftMargin = 30;
81
printSystem.PageSettings.RightMargin = 30;
82
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
83
}
84
85
public ReportCenter(IPrintable Printable, PaperKind paperKind)
86
{
87
printSystem = new PrintingSystem();
88
mReportName = "";
89
mCondition = "";
90
PrintableComponentLink pcl = new PrintableComponentLink();
91
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
92
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
93
pcl.Component = Printable;
94
printSystem.Links.Add(pcl);
95
pcl.CreateDocument();
96
97
PrinterSettingsUsing pst = new PrinterSettingsUsing();
98
pst.UseMargins = false;
99
pst.UsePaperKind = false;
100
printSystem.PageSettings.PaperKind = paperKind;
101
printSystem.PageSettings.PaperName = "A4";
102
printSystem.PageSettings.LeftMargin = 2;
103
printSystem.PageSettings.RightMargin = 2;
104
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
105
}
106
public ReportCenter(IPrintable Printable, string ReportName, PaperKind paperKind)
107
{
108
printSystem = new PrintingSystem();
109
mReportName = ReportName;
110
mCondition = "";
111
PrintableComponentLink pcl = new PrintableComponentLink();
112
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
113
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
114
pcl.Component = Printable;
115
printSystem.Links.Add(pcl);
116
pcl.CreateDocument();
117
118
PrinterSettingsUsing pst = new PrinterSettingsUsing();
119
pst.UseMargins = true;
120
//pst.UsePaperKind = false;
121
printSystem.PageSettings.PaperKind = paperKind;
122
printSystem.PageSettings.PaperName = "A4";
123
printSystem.PageSettings.LeftMargin = 30;
124
printSystem.PageSettings.RightMargin = 30;
125
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
126
}
127
public ReportCenter(IPrintable Printable, string ReportName, string Condition,PaperKind paperKind)
128
{
129
printSystem = new PrintingSystem();
130
mReportName = ReportName;
131
mCondition = Condition;
132
PrintableComponentLink pcl = new PrintableComponentLink();
133
pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
134
pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
135
pcl.Component = Printable;
136
printSystem.Links.Add(pcl);
137
pcl.CreateDocument();
138
139
PrinterSettingsUsing pst = new PrinterSettingsUsing();
140
pst.UseMargins = true;
141
//pst.UsePaperKind = false;
142
printSystem.PageSettings.PaperKind = paperKind;
143
//printSystem.PageSettings.PaperName = "A4";
144
printSystem.PageSettings.LeftMargin = 30;
145
printSystem.PageSettings.RightMargin = 30;
146
printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
147
}
148
149
private void link_CreateMarginalHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
150
{
151
if (mReportName != "")
152
{
153
e.Graph.Font = new Font("宋体", 15, FontStyle.Bold);
154
e.Graph.BackColor = Color.Transparent;
155
RectangleF r = new RectangleF(0, 20, 0, e.Graph.Font.Height + 20);
156
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mReportName, Color.Black, r, BorderSide.None);
157
brick.Alignment = BrickAlignment.Center;
158
brick.AutoWidth = true;
159
}
160
161
if (mCondition != "")
162
{
163
e.Graph.Font = new Font("宋体", 10);
164
e.Graph.BackColor = Color.Transparent;
165
RectangleF r = new RectangleF(0, 50, 0, e.Graph.Font.Height + 20);
166
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mCondition, Color.Black, r, BorderSide.None);
167
brick.Alignment = BrickAlignment.Center;
168
brick.AutoWidth = true;
169
}
170
}
171
private void link_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
172
{
173
string format = "第{0}页 共{1}页";
174
e.Graph.Font = new Font("宋体", 10);
175
e.Graph.BackColor = Color.Transparent;
176
177
RectangleF r = new RectangleF(0, 5, 0, e.Graph.Font.Height + 20);
178
179
PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, format, Color.Black, r, BorderSide.None);
180
brick.Alignment = BrickAlignment.Far;
181
brick.AutoWidth = true;
182
183
brick = e.Graph.DrawPageInfo(PageInfo.DateTime, "打印时间:" + DateTime.Today.ToLongDateString(), Color.Black, r, BorderSide.None);
184
brick.Alignment = BrickAlignment.Near;
185
brick.AutoWidth = true;
186
}public void Print()
187
{
188
printSystem.Print();
189
}
190
191
public void Preview()
192
{
193
FrmPreview frm = new FrmPreview(printSystem);
194
frm.Show();
195
}
196
public void Designe()
197
{}
198
199
200
}
201
}
202

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

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

1
//使用方法
2
ReportCenter rc = new ReportCenter(gControl, lblTitle.Text, strDate);
3
rc.Preview();

2

3
