/
PivotReportGenerator.cs
309 lines (288 loc) · 13.1 KB
/
PivotReportGenerator.cs
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraEditors;
using System.Text;
using DevExpress.Drawing;
using DevExpress.XtraPrinting;
public static class PivotReportGenerator
{
public static XtraReport GenerateReport(PivotGridControl pivot, ReportGeneratorType kind, int columnWidth, bool repeatRowHeader)
{
XtraReport rep = new XtraReport();
rep.Landscape = true;
rep.DataSource = FillDataset(pivot);
rep.DataMember = ((DataSet)rep.DataSource).Tables[0].TableName;
InitBands(rep);
InitStyles(rep);
InitDetailsBasedonXRTable(rep, kind, columnWidth, repeatRowHeader);
return rep;
}
public static DataSet FillDataset(PivotGridControl pivot)
{
DataSet dataSet1 = new DataSet();
dataSet1.DataSetName = "PivotGridColumns";
DataTable dataTable1 = new DataTable();
dataSet1.Tables.Add(dataTable1);
FillDatasetColumns(pivot, dataTable1);
FillDatasetExtracted(pivot, dataTable1);
return dataSet1;
}
#region PreparingDataSet
private static void FillDatasetExtracted(PivotGridControl pivot, DataTable dataTable1)
{
List<object> rowvalues = new List<object>();
string tempRowText = "";
List<PivotGridField> fieldsInRowArea = GetFieldsInArea(pivot, PivotArea.RowArea);
for (int i = 0; i < pivot.Cells.RowCount; i++)
{
PivotCellEventArgs pcea = pivot.Cells.GetCellInfo(0, i);
if (pcea.RowValueType == PivotGridValueType.Value)
{
foreach (PivotGridField item in fieldsInRowArea)
tempRowText += pcea.GetFieldValue(item).ToString() + " | ";//add formatting if it's necessary
tempRowText = tempRowText.Remove(tempRowText.Length - 3, 3);
}
else
tempRowText = pcea.RowValueType.ToString();
rowvalues.Clear();
rowvalues.Add(tempRowText);
tempRowText = "";
for (int j = 0; j < pivot.Cells.ColumnCount; j++)
{
pcea = pivot.Cells.GetCellInfo(j, i);
if (pcea.Value != null)
rowvalues.Add(pcea.Value);
else
rowvalues.Add(DBNull.Value);
}
dataTable1.Rows.Add(rowvalues.ToArray());
}
}
private static void FillDatasetColumns(PivotGridControl pivot, DataTable dataTable1)
{
dataTable1.Columns.Add("RowFields", typeof(string));
StringBuilder sb = new StringBuilder();
List<PivotGridField> fieldsInColumnArea = GetFieldsInArea(pivot, PivotArea.ColumnArea);
bool multipleDataField = pivot.GetFieldsByArea(PivotArea.DataArea).Count > 1;
for (int i = 0; i < pivot.Cells.ColumnCount; i++)
{
PivotCellEventArgs pcea = pivot.Cells.GetCellInfo(i, 0);
foreach (PivotGridField field in pcea.GetColumnFields())
sb.AppendFormat("{0} | ", field.GetDisplayText( pcea.GetFieldValue(field) ));//add formatting if it's necessary
if (multipleDataField)
sb.AppendFormat("{0} | ", pcea.DataField);
if (pcea.ColumnValueType == PivotGridValueType.Value)
sb.Remove(sb.Length - 3, 3);
else
sb.Append(pcea.ColumnValueType.ToString());
dataTable1.Columns.Add(sb.ToString(), typeof(object));
sb.Clear();
}
}
private static List<PivotGridField> GetFieldsInArea(PivotGridControl pivot, PivotArea area)
{
List<PivotGridField> fields = new List<PivotGridField>();
for (int i = 0; i < pivot.Fields.Count; i++)
if (pivot.Fields[i].Area == area)
fields.Add(pivot.Fields[i]);
return fields;
}
#endregion
public static void InitBands(XtraReport rep) {
// Create bands
DetailBand detail = new DetailBand();
PageHeaderBand pageHeader = new PageHeaderBand();
ReportFooterBand reportFooter = new ReportFooterBand();
detail.Height = 20;
reportFooter.Height = 380;
pageHeader.Height = 20;
// Place the bands onto a report
rep.Bands.AddRange(new Band[] { detail, pageHeader, reportFooter });
}
public static void InitStyles(XtraReport rep) {
// Create different odd and even styles
XRControlStyle oddStyle = new XRControlStyle();
XRControlStyle evenStyle = new XRControlStyle();
// Specify the odd style appearance
oddStyle.BackColor = System.Drawing.Color.LightBlue;
oddStyle.StyleUsing.UseBackColor = true;
oddStyle.StyleUsing.UseBorders = false;
oddStyle.Name = "OddStyle";
// Specify the even style appearance
evenStyle.BackColor = System.Drawing.Color.LightPink;
evenStyle.StyleUsing.UseBackColor = true;
evenStyle.StyleUsing.UseBorders = false;
evenStyle.Name = "EvenStyle";
// Add styles to report's style sheet
rep.StyleSheet.AddRange(new XRControlStyle[] { oddStyle, evenStyle });
}
public static void InitDetailsBasedonXRTable(XtraReport rep, ReportGeneratorType kind, float columnWidth, bool repeatRowHeader) {
if (!repeatRowHeader || kind == ReportGeneratorType.SinglePage)
InitDetailsBasedonXRTableWithoutRepeatingRowHeader(rep, kind, columnWidth);
else
InitDetailsBasedonXRTableRepeatingRowHeader(rep, kind, columnWidth);
}
static void InitDetailsBasedonXRTableRepeatingRowHeader(XtraReport rep, ReportGeneratorType kind, float columnWidth) {
DXFont font = new DXFont("Tahoma", 9.75f);
DataTable dataTable = ((DataSet)rep.DataSource).Tables[0];
int processedPage = 0;
float usablePageWidth = rep.PageWidth - (rep.Margins.Left + rep.Margins.Right);
List<float> columnsWidth = null;
if (kind == ReportGeneratorType.FixedColumnWidth)
columnsWidth = DefineColumnsWidth(columnWidth, dataTable.Columns.Count);
else
columnsWidth = GetColumnsBestFitWidth(dataTable, font, rep.ReportUnit);
XRTable tableHeader = null;
XRTable tableDetail = null;
InitNewTableInstancesAt(rep, font, out tableHeader, out tableDetail, new PointF(0, 0));
tableHeader.BeginInit();
tableDetail.BeginInit();
int i = 1;
AddCellsToTables(tableHeader, tableDetail, dataTable.Columns[0], columnsWidth[0], true);
float remainingSpace = usablePageWidth - columnsWidth[0];
do {
if (columnsWidth[i] > remainingSpace) {
processedPage++;
tableHeader.WidthF = usablePageWidth - remainingSpace;
tableDetail.WidthF = usablePageWidth - remainingSpace;
tableHeader.EndInit();
tableDetail.EndInit();
InitNewTableInstancesAt(rep, font, out tableHeader, out tableDetail, new PointF(usablePageWidth * processedPage, 0));
tableHeader.BeginInit();
tableDetail.BeginInit();
AddCellsToTables(tableHeader, tableDetail, dataTable.Columns[0], columnsWidth[0], true);
remainingSpace = usablePageWidth - columnsWidth[0];
}
else {
AddCellsToTables(tableHeader, tableDetail, dataTable.Columns[i], columnsWidth[i], false);
remainingSpace -= columnsWidth[i];
i++;
}
}
while (i < columnsWidth.Count);
tableHeader.WidthF = usablePageWidth - remainingSpace;
tableDetail.WidthF = usablePageWidth - remainingSpace;
tableHeader.EndInit();
tableDetail.EndInit();
}
public static void AddCellsToTables(XRTable header, XRTable detail, DataColumn dc, float columnWidth, bool isFirstColumnInTable) {
XRTableCell headerCell = new XRTableCell();
headerCell.Text = dc.Caption;
XRTableCell detailCell = new XRTableCell();
detailCell.DataBindings.Add("Text", null, dc.Caption);
headerCell.WidthF = columnWidth;
detailCell.WidthF = columnWidth;
if (isFirstColumnInTable) {
headerCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
}
else {
headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
detailCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
}
// Place the cells into the corresponding tables
header.Rows[0].Cells.Add(headerCell);
detail.Rows[0].Cells.Add(detailCell);
}
public static void InitNewTableInstancesAt(XtraReport report, DXFont font, out XRTable header, out XRTable detail, PointF location) {
header = InitXRTable(font, false);
detail = InitXRTable(font, true);
header.LocationF = location;
detail.LocationF = location;
XRTableRow headerRow = new XRTableRow();
header.Rows.Add(headerRow);
XRTableRow detailRow = new XRTableRow();
detail.Rows.Add(detailRow);
report.Bands[BandKind.PageHeader].Controls.Add(header);
report.Bands[BandKind.Detail].Controls.Add(detail);
}
static XRTable InitXRTable(DXFont font, bool withStyles) {
XRTable table = new XRTable();
table.Font = font;
table.Height = 20;
if (withStyles) {
table.EvenStyleName = "EvenStyle";
table.OddStyleName = "OddStyle";
}
return table;
}
static List<float> DefineColumnsWidth(float columnWidth, int count) {
List<float> columnsWidth = new List<float>();
for (int i = 0; i < count; i++)
columnsWidth.Add(columnWidth);
return columnsWidth;
}
static void InitDetailsBasedonXRTableWithoutRepeatingRowHeader(XtraReport rep, ReportGeneratorType kind, float columnWidth) {
DXFont font = new DXFont("Tahoma", 9.75f);
DataSet ds = ((DataSet)rep.DataSource);
int colCount = ds.Tables[0].Columns.Count;
float colWidth = 0;
XRTable tableHeader = null;
XRTable tableDetail = null;
InitNewTableInstancesAt(rep, font, out tableHeader, out tableDetail, new PointF(0, 0));
List<float> columnsWidth = null;
switch (kind) {
case ReportGeneratorType.FixedColumnWidth:
colWidth = columnWidth;
tableHeader.WidthF = columnWidth * colCount;
tableDetail.WidthF = columnWidth * colCount;
break;
case ReportGeneratorType.BestFitColumns:
columnsWidth = GetColumnsBestFitWidth(ds.Tables[0], font, rep.ReportUnit);
colWidth = 0;
tableHeader.WidthF = GetTotalWidth(columnsWidth);
tableDetail.WidthF = tableHeader.Width;
break;
default:
colWidth = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right)) / colCount;
tableHeader.WidthF = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right));
tableDetail.WidthF = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right));
break;
}
tableHeader.BeginInit();
tableDetail.BeginInit();
// Create table cells, fill the header cells with text, bind the cells to data
for (int i = 0; i < colCount; i++) {
AddCellsToTables(tableHeader, tableDetail, ds.Tables[0].Columns[i], kind == ReportGeneratorType.BestFitColumns ? columnsWidth[i] : colWidth, i == 0 ? true : false);
}
tableDetail.EndInit();
tableHeader.EndInit();
// Place the table onto a report's Detail band
}
static float GetTotalWidth(List<float> columnsWidth) {
float i = 0;
foreach (float colWidth in columnsWidth)
i += colWidth;
return i;
}
static List<float> GetColumnsBestFitWidth(DataTable dataTable, DXFont font, ReportUnit unit) {
List<float> optimalColumnWidth = new List<float>();
float maxWidth = 0;
float tempWidth = 0;
for (int i = 1; i < dataTable.Rows.Count; i++) {
tempWidth = MeasureWidth(dataTable.Rows[i][0].ToString(), font, unit);
maxWidth = maxWidth > tempWidth ? maxWidth : tempWidth;
}
optimalColumnWidth.Add(maxWidth);
for (int i = 1; i < dataTable.Columns.Count; i++) {
tempWidth = MeasureWidth(dataTable.Columns[i].ColumnName.ToString(), font, unit);
maxWidth = 50 > tempWidth ? 50 : tempWidth;
optimalColumnWidth.Add(maxWidth);
}
return optimalColumnWidth;
}
static float MeasureWidth(string candidate, DXFont font, ReportUnit unit) {
return BestSizeEstimator.GetBoundsToFitText(candidate, new BrickStyle() { Font = font }, unit).Width;
}
}
public enum ReportGeneratorType
{
SinglePage,
FixedColumnWidth,
BestFitColumns,
}