内容摘要:最近花了很多工夫研究了C# 2005的RDLC报表,个人感觉功能是很强大,但是编码却很费力,并且这方面的示例也实在是太少了。以下是我整理的一报表控件,在此与大家分享。一、改进后的ReportViewer1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using Microsoft.Repor。
最近花了很多工夫研究了C# 2005的RDLC报表,个人感觉功能是很强大,但是编码却很费力,并且这方面的示例也实在是太少了。以下是我整理的一报表控件,在此与大家分享。
一、改进后的ReportViewer
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Microsoft.Reporting.WinForms;
5 using System.ComponentModel;
6 using System.IO;
7
8 namespace FBS.GDTVMember.Report
9 {
10 public class ReportViewer : Microsoft.Reporting.WinForms.ReportViewer
11 {
12 private string _rcfile;
13 private ReportConfig _rpc;
14 private object _datasource;
15 private MemoryStream m_rdl;
16
17 public ReportViewer() :base()
18 {
19
20 }
21
22 /// <summary>
23 /// 配置文件
24 /// </summary>
25 [DefaultValue("")]
26 public string Filename
27 {
28 get { return _rcfile; }
29 set
30 {
31 _rcfile = value;
32 _rpc = new ReportConfig(_rcfile);
33 }
34 }
35
36 /// <summary>
37 /// 报表配置
38 /// </summary>
39 public ReportConfig ReportConfig
40 {
41 get { return _rpc; }
42 }
43
44 /// <summary>
45 /// 数据源
46 /// </summary>
47 public object DataSource
48 {
49 get { return _datasource; }
50 set { _datasource = value; }
51 }
52
53 /// <summary>
54 /// 显示报表
55 /// </summary>
56 public void ShowReport()
57 {
58 if (m_rdl != null)
59 m_rdl.Dispose();
60 m_rdl = GenerateRdl();
61
62 Reset();
63 LocalReport.LoadReportDefinition(m_rdl);
64 LocalReport.DataSources.Add(new ReportDataSource("FaibLists", _datasource));
65 RefreshReport();
66 }
67
68 /// <summary>
69 /// 生成Rdl流
70 /// </summary>
71 /// <returns></returns>
72 private MemoryStream GenerateRdl()
73 {
74 MemoryStream ms = new MemoryStream();
75 RdlGenerator gen = new RdlGenerator();
76 gen.ReportConfig = _rpc;
77 gen.WriteXml(ms);
78 ms.Position = 0;
79 return ms;
80 }
81
82 }
83 }
84二、Rdl生成类
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Xml.Serialization;
6
7 namespace FBS.GDTVMember.Report
8 {
9 public class RdlGenerator
10 {
11 private ReportConfig _rpc;
12
13 /// <summary>
14 /// 报表配置
15 /// </summary>
16 public ReportConfig ReportConfig
17 {
18 get { return _rpc; }
19 set { _rpc = value; }
20 }
21
22 /// <summary>
23 /// 创建报表
24 /// </summary>
25 /// <returns></returns>
26 private Rdl.Report CreateReport()
27 {
28 Rdl.Report report = new Rdl.Report();
29 string w = "", h = "", lm = "", tm = "", rm = "", bm = "";
30 //设置报表页面
31 if(!string.IsNullOrEmpty(_rpc.Unit))
32 {
33 w = _rpc.Width + _rpc.Unit;
34 h = _rpc.Height + _rpc.Unit;
35 lm = _rpc.LeftMargin + _rpc.Unit;
36 tm = _rpc.TopMargin + _rpc.Unit;
37 rm = _rpc.RightMargin + _rpc.Unit;
38 bm = _rpc.BottomMargin + _rpc.Unit;
39 }
40 else
41 {
42 w = (_rpc.PageSettings.PaperSize.Width / 96.0) + "in";
43 h = (_rpc.PageSettings.PaperSize.Height / 96.0) + "in";
44 lm = (_rpc.LeftMargin / 96.0) + "in";
45 tm = (_rpc.TopMargin / 96.0) + "in";
46 rm = (_rpc.RightMargin / 96.0) + "in";
47 bm = (_rpc.BottomMargin / 96.0) + "in";
48 }
49 report.Items = new object[]
50 {
51 CreateDataSources(),
52 CreateHeader(),
53 CreateBody(),
54 CreateFooter(),
55 CreateDataSets(),
56 w,
57 h,
58 lm,
59 tm,
60 rm,
61 bm,
62 };
63 report.ItemsElementName = new Rdl.ItemsChoiceType37[]
64 {
65 Rdl.ItemsChoiceType37.DataSources,
66 Rdl.ItemsChoiceType37.PageHeader,
67 Rdl.ItemsChoiceType37.Body,
68 Rdl.ItemsChoiceType37.PageFooter,
69 Rdl.ItemsChoiceType37.DataSets,
70 Rdl.ItemsChoiceType37.Width,
71 Rdl.ItemsChoiceType37.PageHeight,
72 Rdl.ItemsChoiceType37.LeftMargin,
73 Rdl.ItemsChoiceType37.TopMargin,
74 Rdl.ItemsChoiceType37.RightMargin,
75 Rdl.ItemsChoiceType37.BottomMargin,
76 };
77 return report;
78 }
79
80 #region 数据源
81 /// <summary>
82 /// 数据源
83 /// </summary>
84 /// <returns></returns>
85 private Rdl.DataSourcesType CreateDataSources()
86 {
87 Rdl.DataSourcesType dataSources = new Rdl.DataSourcesType();
88 dataSources.DataSource = new Rdl.DataSourceType[] { CreateDataSource() };
89 return dataSources;
90 }
91
92 private Rdl.DataSourceType CreateDataSource()
93 {
94 Rdl.DataSourceType dataSource = new Rdl.DataSourceType();
95 dataSource.Name = "FaibLists";
96 dataSource.Items = new object[] { CreateConnectionProperties() };
97 return dataSource;
98 }
99
100 private Rdl.ConnectionPropertiesType CreateConnectionProperties()
101 {
102 Rdl.ConnectionPropertiesType connectionProperties = new Rdl.ConnectionPropertiesType();
103 connectionProperties.Items = new object[]
104 {
105 "",
106 "SQL",
107 };
108 connectionProperties.ItemsElementName = new Rdl.ItemsChoiceType[]
109 {
110 Rdl.ItemsChoiceType.ConnectString,
111 Rdl.ItemsChoiceType.DataProvider,
112 };
113 return connectionProperties;
114 }
115 #endregion
116
117 #region 主体
118 /// <summary>
119 /// 报表主体
120 /// </summary>
121 /// <returns></returns>
122 private Rdl.BodyType CreateBody()
123 {
124 Rdl.BodyType body = new Rdl.BodyType();
125 body.Items = new object[]
126 {
127 CreateReportItems(),
128 "1in",
129 };
130 body.ItemsElementName = new Rdl.ItemsChoiceType30[]
131 {
132 Rdl.ItemsChoiceType30.ReportItems,
133 Rdl.ItemsChoiceType30.Height,
134 };
135 return body;
136 }
137
138 private Rdl.ReportItemsType CreateReportItems()
139 {
140 Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
141 TableRdlGenerator tableGen = new TableRdlGenerator();
142 tableGen.Fields = _rpc.DataItem;
143 reportItems.Items = new object[] { tableGen.CreateTable() };
144 return reportItems;
145 }
146 #endregion
147
148 #region 页头页尾
149 private Rdl.PageHeaderFooterType CreateHeader()
150 {
151 Rdl.PageHeaderFooterType header = new FBS.GDTVMember.Report.Rdl.PageHeaderFooterType();
152 HeaderFooterRdlGenerator headerGen = new HeaderFooterRdlGenerator();
153 headerGen.Fields = _rpc.Header;
154
155 header.Items = new object[]
156 {
157 (_rpc.HeadHeight / 96.0) + "in",
158 true,
159 true,
160 headerGen.CreateItems(),
161 };
162 header.ItemsElementName = new Rdl.ItemsChoiceType34[]
163 {
164 Rdl.ItemsChoiceType34.Height,
165 Rdl.ItemsChoiceType34.PrintOnFirstPage,
166 Rdl.ItemsChoiceType34.PrintOnLastPage,
167 Rdl.ItemsChoiceType34.ReportItems
168 };
169 return header;
170 }
171
172 private Rdl.PageHeaderFooterType CreateFooter()
173 {
174 Rdl.PageHeaderFooterType footer = new FBS.GDTVMember.Report.Rdl.PageHeaderFooterType();
175 HeaderFooterRdlGenerator footerGen = new HeaderFooterRdlGenerator();
176 footerGen.Fields = _rpc.Footer;
177
178 footer.Items = new object[]
179 {
180 (_rpc.FootHeight / 96.0) + "in",
181 true,
182 true,
183 footerGen.CreateItems(),
184 };
185 footer.ItemsElementName = new Rdl.ItemsChoiceType34[]
186 {
187 Rdl.ItemsChoiceType34.Height,
188 Rdl.ItemsChoiceType34.PrintOnFirstPage,
189 Rdl.ItemsChoiceType34.PrintOnLastPage,
190 Rdl.ItemsChoiceType34.ReportItems
191 };
192 return footer;
193 }
194 #endregion
195
196 #region 数据集
197 private Rdl.DataSetsType CreateDataSets()
198 {
199 Rdl.DataSetsType dataSets = new Rdl.DataSetsType();
200 dataSets.DataSet = new Rdl.DataSetType[] { CreateDataSet() };
201 return dataSets;
202 }
203
204 private Rdl.DataSetType CreateDataSet()
205 {
206 Rdl.DataSetType dataSet = new Rdl.DataSetType();
207 dataSet.Name = "FaibLists";
208 dataSet.Items = new object[] { CreateQuery(), CreateFields() };
209 return dataSet;
210 }
211
212 private Rdl.QueryType CreateQuery()
213 {
214 Rdl.QueryType query = new Rdl.QueryType();
215 query.Items = new object[]
216 {
217 "FaibLists",
218 "",
219 };
220 query.ItemsElementName = new Rdl.ItemsChoiceType2[]
221 {
222 Rdl.ItemsChoiceType2.DataSourceName,
223 Rdl.ItemsChoiceType2.CommandText,
224 };
225 return query;
226 }
227
228 private Rdl.FieldsType CreateFields()
229 {
230 Rdl.FieldsType fields = new Rdl.FieldsType();
231 Dictionary<string, TextItem> m_fields = _rpc.DataItem;
232 fields.Field = new Rdl.FieldType[m_fields.Count];
233 int i = 0;
234 foreach (string key in m_fields.Keys)
235 {
236 fields.Field[i++] = CreateField(m_fields[key]);
237 }
238
239 return fields;
240 }
241
242 private Rdl.FieldType CreateField(TextItem item)
243 {
244 Rdl.FieldType field = new Rdl.FieldType();
245 field.Name = item.DataMember;
246 field.Items = new object[] { item.DataMember };
247 field.ItemsElementName = new Rdl.ItemsChoiceType1[] { Rdl.ItemsChoiceType1.DataField };
248 return field;
249 }
250 #endregion
251
252 public void WriteXml(Stream stream)
253 {
254 XmlSerializer serializer = new XmlSerializer(typeof(Rdl.Report));
255 serializer.Serialize(stream, CreateReport());
256 }
257 }
258 }
259三、报表主体生成类TableRdlGenerator
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5
6 namespace FBS.GDTVMember.Report
7 {
8 public class TableRdlGenerator
9 {
10 private Dictionary<string, TextItem> m_fields;
11
12 public Dictionary<string, TextItem> Fields
13 {
14 get { return m_fields; }
15 set { m_fields = value; }
16 }
17
18 public Rdl.TableType CreateTable()
19 {
20 //定义表格
21 Rdl.TableType table = new Rdl.TableType();
22 table.Name = "Table1";
23 table.Items = new object[]
24 {
25 CreateTableColumns(),
26 CreateHeader(),
27 CreateDetails(),
28 };
29 table.ItemsElementName = new Rdl.ItemsChoiceType21[]
30 {
31 Rdl.ItemsChoiceType21.TableColumns,
32 Rdl.ItemsChoiceType21.Header,
33 Rdl.ItemsChoiceType21.Details,
34 };
35 return table;
36 }
37
38 private Rdl.HeaderType CreateHeader()
39 {
40 Rdl.HeaderType header = new Rdl.HeaderType();
41 header.Items = new object[]
42 {
43 CreateHeaderTableRows(),
44 true,
45 };
46 header.ItemsElementName = new Rdl.ItemsChoiceType20[]
47 {
48 Rdl.ItemsChoiceType20.TableRows,
49 Rdl.ItemsChoiceType20.RepeatOnNewPage,
50 };
51 return header;
52 }
53
54 private Rdl.TableRowsType CreateHeaderTableRows()
55 {
56 Rdl.TableRowsType headerTableRows = new Rdl.TableRowsType();
57 headerTableRows.TableRow = new Rdl.TableRowType[] { CreateHeaderTableRow() };
58 return headerTableRows;
59 }
60
61 private Rdl.TableRowType CreateHeaderTableRow()
62 {
63 Rdl.TableRowType headerTableRow = new Rdl.TableRowType();
64 headerTableRow.Items = new object[] { CreateHeaderTableCells(), "0.25in" };
65 return headerTableRow;
66 }
67
68 private Rdl.TableCellsType CreateHeaderTableCells()
69 {
70 Rdl.TableCellsType headerTableCells = new Rdl.TableCellsType();
71 headerTableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
72 int i = 0;
73 foreach (string key in m_fields.Keys)
74 {
75 headerTableCells.TableCell[i++] = CreateHeaderTableCell(m_fields[key]);
76 }
77 return headerTableCells;
78 }
79
80 private Rdl.TableCellType CreateHeaderTableCell(TextItem item)
81 {
82 Rdl.TableCellType headerTableCell = new Rdl.TableCellType();
83 headerTableCell.Items = new object[] { CreateHeaderTableCellReportItems(item) };
84 return headerTableCell;
85 }
86
87 private Rdl.ReportItemsType CreateHeaderTableCellReportItems(TextItem item)
88 {
89 Rdl.ReportItemsType headerTableCellReportItems = new Rdl.ReportItemsType();
90 headerTableCellReportItems.Items = new object[] { CreateHeaderTableCellTextbox(item) };
91 return headerTableCellReportItems;
92 }
93
94 private Rdl.TextboxType CreateHeaderTableCellTextbox(TextItem item)
95 {
96 Rdl.TextboxType headerTableCellTextbox = new Rdl.TextboxType();
97 headerTableCellTextbox.Name = item.Text + "_Header";
98 headerTableCellTextbox.Items = new object[]
99 {
100 item.Text,
101 CreateHeaderTableCellTextboxStyle(item),
102 true,
103 };
104 headerTableCellTextbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
105 {
106 Rdl.ItemsChoiceType14.Value,
107 Rdl.ItemsChoiceType14.Style,
108 Rdl.ItemsChoiceType14.CanGrow,
109 };
110 return headerTableCellTextbox;
111 }
112
113 private Rdl.StyleType CreateHeaderTableCellTextboxStyle(TextItem item)
114 {
115 Rdl.StyleType headerTableCellTextboxStyle = new Rdl.StyleType();
116 headerTableCellTextboxStyle.Items = new object[]
117 {
118 item.HeaderFont.Name,
119 item.HeaderFont.Size + "pt",
120 item.HeaderFont.Bold ? "700" : "100",
121 "Center",
122 "Middle",
123 CreateBorderStyle(),
124 };
125 headerTableCellTextboxStyle.ItemsElementName = new Rdl.ItemsChoiceType5[]
126 {
127 Rdl.ItemsChoiceType5.FontFamily,
128 Rdl.ItemsChoiceType5.FontSize,
129 Rdl.ItemsChoiceType5.FontWeight,
130 Rdl.ItemsChoiceType5.TextAlign,
131 Rdl.ItemsChoiceType5.VerticalAlign,
132 Rdl.ItemsChoiceType5.BorderStyle,
133 };
134 return headerTableCellTextboxStyle;
135 }
136
137 private Rdl.DetailsType CreateDetails()
138 {
139 Rdl.DetailsType details = new Rdl.DetailsType();
140 details.Items = new object[] { CreateTableRows() };
141 return details;
142 }
143
144 private Rdl.TableRowsType CreateTableRows()
145 {
146 Rdl.TableRowsType tableRows = new Rdl.TableRowsType();
147 tableRows.TableRow = new Rdl.TableRowType[] { CreateTableRow() };
148 return tableRows;
149 }
150
151 private Rdl.TableRowType CreateTableRow()
152 {
153 Rdl.TableRowType tableRow = new Rdl.TableRowType();
154 tableRow.Items = new object[] { CreateTableCells(), "0.25in" };
155 return tableRow;
156 }
157
158 private Rdl.TableCellsType CreateTableCells()
159 {
160 Rdl.TableCellsType tableCells = new Rdl.TableCellsType();
161 tableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
162 int i = 0;
163 foreach(string key in m_fields.Keys)
164 {
165 tableCells.TableCell[i++] = CreateTableCell(m_fields[key]);
166 }
167 return tableCells;
168 }
169
170 private Rdl.TableCellType CreateTableCell(TextItem item)
171 {
172 Rdl.TableCellType tableCell = new Rdl.TableCellType();
173 tableCell.Items = new object[] { CreateTableCellReportItems(item) };
174 return tableCell;
175 }
176
177 private Rdl.ReportItemsType CreateTableCellReportItems(TextItem item)
178 {
179 Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
180 reportItems.Items = new object[] { CreateTableCellTextbox(item) };
181 return reportItems;
182 }
183
184 private Rdl.TextboxType CreateTableCellTextbox(TextItem item)
185 {
186 Rdl.TextboxType textbox = new Rdl.TextboxType();
187 textbox.Name = item.Key;
188 textbox.Items = new object[]
189 {
190 "=Fields!" + item.DataMember + ".Value",
191 CreateTableCellTextboxStyle(item),
192 true,
193 };
194 textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
195 {
196 Rdl.ItemsChoiceType14.Value,
197 Rdl.ItemsChoiceType14.Style,
198 Rdl.ItemsChoiceType14.CanGrow,
199 };
200 return textbox;
201 }
202
203 private Rdl.StyleType CreateTableCellTextboxStyle(TextItem item)
204 {
205 Rdl.StyleType style = new Rdl.StyleType();
206 style.Items = new object[]
207 {
208 item.Font.Name,
209 item.Font.Size + "pt",
210 item.Font.Bold ? "400" : "100",
211 GetAlign(item.Align.Alignment),
212 "Middle",
213 CreateBorderStyle(),
214 "1pt",
215 "1pt",
216 "1pt",
217 "1pt",
218 };
219 style.ItemsElementName = new Rdl.ItemsChoiceType5[]
220 {
221 Rdl.ItemsChoiceType5.FontFamily,
222 Rdl.ItemsChoiceType5.FontSize,
223 Rdl.ItemsChoiceType5.FontWeight,
224 Rdl.ItemsChoiceType5.TextAlign,
225 Rdl.ItemsChoiceType5.VerticalAlign,
226 Rdl.ItemsChoiceType5.BorderStyle,
227 Rdl.ItemsChoiceType5.PaddingLeft,
228 Rdl.ItemsChoiceType5.PaddingTop,
229 Rdl.ItemsChoiceType5.PaddingRight,
230 Rdl.ItemsChoiceType5.PaddingBottom,
231 };
232 return style;
233 }
234
235 private Rdl.TableColumnsType CreateTableColumns()
236 {
237 Rdl.TableColumnsType tableColumns = new Rdl.TableColumnsType();
238 tableColumns.TableColumn = new Rdl.TableColumnType[m_fields.Count];
239 int i = 0;
240 foreach (string key in m_fields.Keys)
241 {
242 tableColumns.TableColumn[i++] = CreateTableColumn(m_fields[key]);
243 }
244 return tableColumns;
245 }
246
247 private Rdl.TableColumnType CreateTableColumn(TextItem item)
248 {
249 Rdl.TableColumnType tableColumn = new Rdl.TableColumnType();
250 tableColumn.Items = new object[] { (item.Rect.Width / 96.0) + "in" };
251 return tableColumn;
252 }
253
254 private Rdl.BorderColorStyleWidthType CreateBorderStyle()
255 {
256 Rdl.BorderColorStyleWidthType bstyle = new Rdl.BorderColorStyleWidthType();
257 bstyle.Items = new object[]
258 {
259 "Solid",
260 };
261 bstyle.ItemsElementName = new Rdl.ItemsChoiceType3[]
262 {
263 Rdl.ItemsChoiceType3.Default,
264 };
265 return bstyle;
266 }
267
268 private string GetVAlign(StringAlignment sformat)
269 {
270 switch (sformat)
271 {
272 case StringAlignment.Center: return "Middle";
273 case StringAlignment.Far: return "Bottom";
274 default: return "Top";
275 }
276 }
277
278 private string GetAlign(StringAlignment sformat)
279 {
280 switch (sformat)
281 {
282 case StringAlignment.Center: return "Center";
283 case StringAlignment.Far: return "Right";
284 default: return "Left";
285 }
286 }
287 }
288 }
四、页头页尾生成类HeaderFooterRdlGenerator
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5
6 namespace FBS.GDTVMember.Report
7 {
8 public class HeaderFooterRdlGenerator
9 {
10 private Dictionary<string, TextItem> m_fields;
11 private int m_height;
12
13 public Dictionary<string, TextItem> Fields
14 {
15 get { return m_fields; }
16 set { m_fields = value; }
17 }
18
19 public int Height
20 {
21 get { return m_height; }
22 set { m_height = value; }
23 }
24
25 public Rdl.ReportItemsType CreateItems()
26 {
27 Rdl.ReportItemsType items = new Rdl.ReportItemsType();
28 items.Items = new Rdl.TextboxType[m_fields.Count];
29 int i = 0;
30 foreach (string key in m_fields.Keys)
31 {
32 items.Items[i++] = CreateTextBox(m_fields[key]);
33 }
34 return items;
35 }
36
37 private Rdl.TextboxType CreateTextBox(TextItem item)
38 {
39 Rdl.TextboxType textbox = new Rdl.TextboxType();
40 textbox.Name = item.Key;
41 textbox.Items = new object[]
42 {
43 item.Text,
44 CreateTextboxStyle(item),
45 true,
46 (item.Rect.Left / 96.0) + "in",
47 (item.Rect.Top / 96.0) + "in",
48 (item.Rect.Width / 96.0) + "in",
49 (item.Rect.Height / 96.0) + "in",
50 };
51 textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
52 {
53 Rdl.ItemsChoiceType14.Value,
54 Rdl.ItemsChoiceType14.Style,
55 Rdl.ItemsChoiceType14.CanGrow,
56 Rdl.ItemsChoiceType14.Left,
57 Rdl.ItemsChoiceType14.Top,
58 Rdl.ItemsChoiceType14.Width,
59 Rdl.ItemsChoiceType14.Height,
60 };
61 return textbox;
62 }
63
64 private Rdl.StyleType CreateTextboxStyle(TextItem item)
65 {
66 Rdl.StyleType style = new Rdl.StyleType();
67 style.Items = new object[]
68 {
69 GetAlign(item.Align.Alignment),
70 GetVAlign(item.Align.LineAlignment),
71 item.Font.Name,
72 item.Font.Size + "pt",
73 item.Font.Bold ? "700" : "100",
74 };
75 style.ItemsElementName = new Rdl.ItemsChoiceType5[]
76 {
77 Rdl.ItemsChoiceType5.TextAlign,
78 Rdl.ItemsChoiceType5.VerticalAlign,
79 Rdl.ItemsChoiceType5.FontFamily,
80 Rdl.ItemsChoiceType5.FontSize,
81 Rdl.ItemsChoiceType5.FontWeight,
82 };
83 return style;
84 }
85
86 private string GetVAlign(StringAlignment sformat)
87 {
88 switch(sformat)
89 {
90 case StringAlignment.Center: return "Middle";
91 case StringAlignment.Far: return "Bottom";
92 default: return "Top";
93 }
94 }
95
96 private string GetAlign(StringAlignment sformat)
97 {
98 switch (sformat)
99 {
100 case StringAlignment.Center: return "Center";
101 case StringAlignment.Far: return "Right";
102 default: return "Left";
103 }
104 }
105 }
106 }
107
五、报表配置类ReportConfig
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Drawing.Printing;
5 using System.Text;
6 using System.Reflection;
7 using System.Xml;
8
9 namespace FBS.GDTVMember.Report
10 {
11 /// <summary>
12 /// 报表配置
13 /// </summary>
14 public class ReportConfig
15 {
16 private string _filename;
17 private bool _autosize;
18 private PageSettings _pset = new PageSettings();
19 private Dictionary<string, TextItem> _header;
20 private Dictionary<string, TextItem> _dataitem;
21 private Dictionary<string, TextItem> _footer;
22 private Dictionary<string, object> _cusmdata;
23 private float _headheight;
24 private float _footheight;
25 private float _width, _height, _lm, _tm, _rm, _bm;
26 private string _unit;
27
28 public ReportConfig()
29 {
30 }
31
32 public ReportConfig(string frpFile)
33 {
34 _filename = frpFile;
35 XmlDocument xdoc = new XmlDocument();
36 xdoc.Load(frpFile);
37 XmlNode pnode = xdoc.SelectSingleNode("//configuration/pageset");
38 ReportConfig.GetAttribute(pnode, "unit", ref _unit);
39 if(string.IsNullOrEmpty(_unit)) //没有单位
40 {
41 //页面大小
42 int w = 0, h = 0;
43 ReportConfig.GetAttribute(pnode, "width", ref w);
44 ReportConfig.GetAttribute(pnode, "height", ref h);
45 _autosize = h == 0;
46 _pset.PaperSize = new PaperSize(frpFile, w, h);
47 //页边距
48 int m_left = 0, m_top = 0, m_right = 0, m_bottom = 0;
49 ReportConfig.GetAttribute(pnode, "margin-left", ref m_left);
50 ReportConfig.GetAttribute(pnode, "margin-top", ref m_top);
51 ReportConfig.GetAttribute(pnode, "margin-right", ref m_right);
52 ReportConfig.GetAttribute(pnode, "margin-bottom", ref m_bottom);
53 _pset.Margins = new Margins(m_left, m_right, m_top, m_bottom);
54 }
55 else
56 {
57 ReportConfig.GetAttribute(pnode, "width", ref _width);
58 ReportConfig.GetAttribute(pnode, "height", ref _height);
59 ReportConfig.GetAttribute(pnode, "margin-left", ref _lm);
60 ReportConfig.GetAttribute(pnode, "margin-top", ref _tm);
61 ReportConfig.GetAttribute(pnode, "margin-right", ref _rm);
62 ReportConfig.GetAttribute(pnode, "margin-bottom", ref _bm);
63 }
64 //头
65 pnode = xdoc.SelectSingleNode("//configuration/header");
66 ReportConfig.GetAttribute(pnode, "height", ref _headheight);
67 //字段
68 foreach (XmlNode node in xdoc.SelectNodes("//configuration/header/textitem"))
69 {
70 TextItem item = new TextItem(node);
71 if (_header == null)
72 {
73 _header = new Dictionary<string, TextItem>();
74 }
75 _header.Add(item.Key, item);
76 }
77 //尾
78 pnode = xdoc.SelectSingleNode("//configuration/footer");
79 ReportConfig.GetAttribute(pnode, "height", ref _footheight);
80 //字段
81 foreach (XmlNode node in xdoc.SelectNodes("//configuration/footer/textitem"))
82 {
83 TextItem item = new TextItem(node);
84 if (_footer == null)
85 {
86 _footer = new Dictionary<string, TextItem>();
87 }
88 _footer.Add(item.Key, item);
89 }
90 //数据字段
91 foreach (XmlNode node in xdoc.SelectNodes("//configuration/dataitem/textitem"))
92 {
93 TextItem item = new TextItem(node);
94 if(_dataitem == null)
95 {
96 _dataitem = new Dictionary<string, TextItem>();
97 }
98 _dataitem.Add(item.Key, item);
99 }
100 }
101
102 public Dictionary<string, object> CustomData
103 {
104 get {
105 if (_cusmdata == null)
106 _cusmdata = new Dictionary<string, object>();
107 return _cusmdata;
108 }
109 }
110
111 public Dictionary<string, TextItem> Header
112 {
113 get { return _header; }
114 }
115
116 public Dictionary<string, TextItem> DataItem
117 {
118 get { return _dataitem; }
119 }
120
121 public Dictionary<string, TextItem> Footer
122 {
123 get { return _footer; }
124 }
125
126 public PageSettings PageSettings
127 {
128 get { return _pset; }
129 }
130
131 public float HeadHeight
132 {
133 get { return _headheight; }
134 }
135
136 public float FootHeight
137 {
138 get { return _footheight; }
139 }
140
141 public float Width
142 {
143 get { return _width; }
144 }
145
146 public float Height
147 {
148 get { return _height; }
149 }
150
151 public float LeftMargin
152 {
153 get { return _lm; }
154 }
155
156 public float TopMargin
157 {
158 get { return _tm; }
159 }
160
161 public float RightMargin
162 {
163 get { return _rm; }
164 }
165
166 public float BottomMargin
167 {
168 get { return _bm; }
169 }
170
171 public bool AutoSize
172 {
173 get { return _autosize; }
174 set { _autosize = value; }
175 }
176
177 public string Unit
178 {
179 get { return _unit; }
180 set { _unit = value; }
181 }
182
183 /// <summary>
184 /// 从一个类获取数据
185 /// </summary>
186 /// <param name="data"></param>
187 public void DataFromDataItem(object data)
188 {
189 Type type = data.GetType();
190 foreach (PropertyInfo pinfo in type.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public))
191 {
192 if (CustomData.ContainsKey(pinfo.Name))
193 continue;
194 object value = type.InvokeMember(pinfo.Name, BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic, null, data, null);
195 if(value != null)
196 {
197 CustomData.Add(pinfo.Name, value);
198 }
199 }
200 }
201
202 internal static void GetAttribute(XmlNode node, string key, ref string value)
203 {
204 if (node.Attributes[key] != null)
205 {
206 value = node.Attributes[key].Value;
207 }
208 }
209
210 internal static void GetAttribute(XmlNode node, string key, ref int value)
211 {
212 if (node.Attributes[key] != null)
213 {
214 value = int.Parse(node.Attributes[key].Value);
215 }
216 }
217
218 internal static void GetAttribute(XmlNode node, string key, ref float value)
219 {
220 if (node.Attributes[key] != null)
221 {
222 value = float.Parse(node.Attributes[key].Value);
223 }
224 }
225 }
226 }
227
六、文本项类TextItem
1 using System;
2 using System.Drawing;
3 using System.Drawing.Printing;
4 using System.Text;
5 using System.Xml;
6
7 namespace FBS.GDTVMember.Report
8 {
9 [Serializable]
10 public class TextItem
11 {
12 private string _text;
13 private string _key;
14 private string _datamember;
15 private Rectangle _rect;
16 private StringFormat _align;
17 private bool _count;
18 private string _format;
19 internal decimal _sum;
20 private Font _font = System.Windows.Forms.Control.DefaultFont;
21 private Font _headerfont = System.Windows.Forms.Control.DefaultFont;
22
23 public TextItem(XmlNode node)
24 {
25 _key = node.Attributes["key"].Value;
26 if(!string.IsNullOrEmpty(node.InnerText))
27 {
28 _text = node.InnerText;
29 }
30 else
31 {
32 ReportConfig.GetAttribute(node, "text", ref _text);
33 }
34 ReportConfig.GetAttribute(node, "data", ref _datamember);
35 ReportConfig.GetAttribute(node, "format", ref _format);
36 string font_name = _font.Name;
37 string font_bold = "";
38 string font_italic = "";
39 string font_underline = "";
40 float font_size = Font.Size;
41 FontStyle fsty = FontStyle.Regular;
42 ReportConfig.GetAttribute(node, "font-name", ref font_name);
43 ReportConfig.GetAttribute(node, "font-size", ref font_size);
44 ReportConfig.GetAttribute(node, "font-bold", ref font_bold);
45 ReportConfig.GetAttribute(node, "font-italic", ref font_italic);
46 ReportConfig.GetAttribute(node, "font-underline", ref font_underline);
47 if (font_bold.Equals("1"))
48 {
49 fsty |= FontStyle.Bold;
50 }
51 if (font_italic.Equals("1"))
52 {
53 fsty |= FontStyle.Italic;
54 }
55 if (font_underline.Equals("1"))
56 {
57 fsty |= FontStyle.Underline;
58 }
59 _font = new Font(font_name, font_size, fsty);
60
61 font_name = _font.Name;
62 font_bold = "";
63 font_italic = "";
64 font_underline = "";
65 font_size = Font.Size;
66 fsty = FontStyle.Regular;
67 ReportConfig.GetAttribute(node, "header-font-name", ref font_name);
68 ReportConfig.GetAttribute(node, "header-font-size", ref font_size);
69 ReportConfig.GetAttribute(node, "header-font-bold", ref font_bold);
70 ReportConfig.GetAttribute(node, "header-font-italic", ref font_italic);
71 ReportConfig.GetAttribute(node, "header-font-underline", ref font_underline);
72 if (font_bold.Equals("1"))
73 {
74 fsty |= FontStyle.Bold;
75 }
76 if (font_italic.Equals("1"))
77 {
78 fsty |= FontStyle.Italic;
79 }
80 if (font_underline.Equals("1"))
81 {
82 fsty |= FontStyle.Underline;
83 }
84 _headerfont = new Font(font_name, font_size, fsty);
85
86 int left = 0, top = 0, width = 0, height = 0;
87 ReportConfig.GetAttribute(node, "left", ref left);
88 ReportConfig.GetAttribute(node, "top", ref top);
89 ReportConfig.GetAttribute(node, "width", ref width);
90 ReportConfig.GetAttribute(node, "height", ref height);
91 _rect = new Rectangle(left, top, width, height);
92
93 string align = "left";
94 ReportConfig.GetAttribute(node, "align", ref align);
95 string valign = "top";
96 ReportConfig.GetAttribute(node, "valign", ref valign);
97 _align = new StringFormat();
98 if (align.Equals("right", StringComparison.OrdinalIgnoreCase))
99 {
100 _align.Alignment = StringAlignment.Far;
101 }
102 else if (align.Equals("center", StringComparison.OrdinalIgnoreCase))
103 {
104 _align.Alignment = StringAlignment.Center;
105 }
106 if (valign.Equals("bottom", StringComparison.OrdinalIgnoreCase))
107 {
108 _align.LineAlignment = StringAlignment.Far;
109 }
110 else if (valign.Equals("middle", StringComparison.OrdinalIgnoreCase))
111 {
112 _align.LineAlignment = StringAlignment.Center;
113 }
114
115 string count = "";
116 ReportConfig.GetAttribute(node, "count", ref count);
117 if(count.Equals("1"))
118 {
119 _count = true;
120 }
121 }
122
123 public string Key
124 {
125 get { return _key; }
126 }
127
128 public string Text
129 {
130 get { return _text; }
131 set { _text = value; }
132 }
133
134 public string DataMember
135 {
136 get { return _datamember; }
137 }
138
139 public string DataFormat
140 {
141 get { return _format; }
142 }
143
144 public StringFormat Align
145 {
146 get { return _align; }
147 }
148
149 public Rectangle Rect
150 {
151 get { return _rect; }
152 set { _rect = value; }
153 }
154
155 public bool Count
156 {
157 get { return _count; }
158 }
159
160 public Font Font
161 {
162 get { return _font; }
163 }
164
165 public Font HeaderFont
166 {
167 get { return _headerfont; }
168 }
169 }
170 }
七、报表配置示例
1 <?xml version="1.0" encoding="gb2312"?>
2 <configuration>
3 <pageset width="29.7" height="21" unit="cm" margin-left="1" margin-top="1" margin-right="1" margin-bottom="1" />
4 <header height="90">
5 <textitem key="ShopName" left="5" top="6" width="960" height="20" align="center" font-name="宋体" font-size="16" font-bold="1" text="" />
6 <textitem key="Title" left="5" top="30" width="960" height="20" align="center" font-name="宋体" font-size="16" font-bold="1" text="消费明细报表" />
7 <textitem key="Admin" left="0" top="60" width="300" height="24" text="" />
8 <textitem key="Date" align="right" left="0" top="60" width="960" height="24" text="" />
9 </header>
10 <dataitem>
11 <textitem key="Code" text="单号" data="Code" width="90" />
12 <textitem key="Card" text="卡号" data="Card" width="100" />
13 <textitem key="MemberName" text="姓名" data="MemberName" width="60" />
14 <textitem key="ProductName" text="消费项目" data="ProductName" width="220" />
15 <textitem key="Price" text="单价" data="Price" width="60" align="right" />
16 <textitem key="Discount" text="折扣" data="Discount" width="60" align="right" />
17 <textitem key="Quantity" text="数量" data="Quantity" width="60" align="right" />
18 <textitem key="TCount" text="金额合计" data="TCount" width="60" align="right" />
19 <textitem key="Score" text="商铺积分" data="Score" width="60" align="right" />
20 <textitem key="MasterScore" text="平台积分" data="MasterScore" width="60" align="right" />
21 <textitem key="AddTime" text="消费日期" data="AddTime" width="124" />
22 </dataitem>
23 <footer height="40">
24 <textitem key="Count" left="5" top="10" width="400" height="24" text="" />
25 <textitem key="Page" align="right" left="800" top="10" width="160" height="24">="第" & Globals!PageNumber & "页 共" & Globals!TotalPages & "页"</textitem>
26 </footer>
27 </configuration>