-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathExportingGridViewModel.cs
250 lines (223 loc) · 9.71 KB
/
ExportingGridViewModel.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
#region Copyright Syncfusion Inc. 2001 - 2024
// Copyright Syncfusion Inc. 2001 - 2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
namespace syncfusion.olapgriddemos.wpf
{
using Syncfusion.Olap.Reports;
using Syncfusion.Olap.Manager;
using Syncfusion.Windows.Grid.Olap.Common;
using Syncfusion.Windows.Shared;
using Microsoft.Win32;
using Syncfusion.Windows.Grid.Olap.Converter;
using System.Windows;
using System;
//github.com/ <summary>
//github.com/ Interaction logic for OlapGrid view.
//github.com/ </summary>
public class ExportingGridViewModel : NotificationObject, IDisposable
{
#region Members
//github.com/ <summary>
//github.com/ Shared connection string.
//github.com/ </summary>
public static string ConnectionString;
private OlapDataManager olapDataManager;
private ExportingGridStyleInfo exportSytleInfo;
private DelegateCommand<object> exportCommand;
#endregion
#region Constructor
//github.com/ <summary>
//github.com/ Initializes a new instance of the <see cref="ExportingGridViewModel"/> class.
//github.com/ </summary>
public ExportingGridViewModel()
{
ConnectionString = KPIModel.Initialize(System.IO.Path.GetFullPath(@"Assets\Config\OLAPSample.config"));
olapDataManager = new OlapDataManager(ConnectionString);
olapDataManager.SetCurrentReport(LoadReports());
}
#endregion
#region Properties
//github.com/ <summary>
//github.com/ Gets or sets the grid data manager.
//github.com/ </summary>
//github.com/ <value>The grid data manager.</value>
public OlapDataManager GridDataManager
{
get { return olapDataManager; }
set { olapDataManager = value; }
}
//github.com/ <summary>
//github.com/ Gets or sets the grid layout.
//github.com/ </summary>
//github.com/ <value>The grid layout.</value>
public Syncfusion.Olap.Engine.GridLayout GridLayout { get; set; }
//github.com/ <summary>
//github.com/ Gets or sets the export style info.
//github.com/ </summary>
//github.com/ <value>The export style info.</value>
public ExportingGridStyleInfo ExportGridStyleInfo
{
get
{
exportSytleInfo = exportSytleInfo ?? new ExportingGridStyleInfo();
return exportSytleInfo;
}
set
{
exportSytleInfo = value;
RaisePropertyChanged("ExportGridStyleInfo");
}
}
//github.com/ <summary>
//github.com/ Gets or sets the export command.
//github.com/ </summary>
//github.com/ <value>The export command.</value>
public DelegateCommand<object> ExportCommand
{
get
{
exportCommand = exportCommand ?? new DelegateCommand<object>(DoExport);
return exportCommand;
}
set { exportCommand = value; }
}
#endregion
#region Methods
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing && this.olapDataManager != null)
this.olapDataManager.Dispose();
}
private void DoExport(object parm)
{
if (parm != null)
{
if (parm.ToString().Equals("Export to Excel"))
{
try
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.AddExtension = true;
savedialog.FileName = "Sample";
savedialog.DefaultExt = "xls";
savedialog.Filter = "Excel file (.xls)|*.xls";
if (savedialog.ShowDialog() == true)
{
GridExcelExport gridExcelExport = new GridExcelExport(this.GridDataManager.PivotEngine, this.ExportGridStyleInfo, this.GridLayout, savedialog.DefaultExt, false);
gridExcelExport.Export(savedialog.FileName);
MessageBox.Show("Exported successfully!.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error on Excel export.\nException Message: " + ex.Message + "\nStack Trace: " + ex.StackTrace, "Export error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else if (parm.ToString().Equals("Export to Word"))
{
try
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.AddExtension = true;
savedialog.FileName = "Sample";
savedialog.DefaultExt = "Doc";
savedialog.Filter = "Word file (.Doc)|*.Doc";
if (savedialog.ShowDialog() == true)
{
GridWordExport gridWordExport = new GridWordExport(this.GridDataManager.PivotEngine, this.GridLayout);
gridWordExport.Export(savedialog.FileName, this.ExportGridStyleInfo);
MessageBox.Show("Exported successfully!.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error on Word export.\nException Message: " + ex.Message + "\nStack Trace: " + ex.StackTrace, "Export error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else if (parm.ToString().Equals("Export to Pdf"))
{
try
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.AddExtension = true;
savedialog.FileName = "Sample";
savedialog.DefaultExt = "pdf";
savedialog.Filter = "Pdf file (.pdf)|*.pdf";
if (savedialog.ShowDialog() == true)
{
GridPdfExport gridPdfExport = new GridPdfExport(this.GridDataManager.PivotEngine, this.ExportGridStyleInfo);
gridPdfExport.Export(savedialog.FileName);
MessageBox.Show("Exported successfully!.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error on PDF export.\nException Message: " + ex.Message + "\nStack Trace: " + ex.StackTrace, "Export error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else if (parm.ToString().Equals("Export to CSV"))
{
try
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.AddExtension = true;
savedialog.FileName = "Sample";
savedialog.DefaultExt = "CSV";
savedialog.Filter = "Csv file (.csv)|*.csv";
if (savedialog.ShowDialog() == true)
{
GridCsvExport gridCsvExport = new GridCsvExport(this.GridDataManager.PivotEngine);
gridCsvExport.Export(savedialog.FileName);
MessageBox.Show("CSV document exported successfully!.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error on CSV export.\nException Message: " + ex.Message + "\nStack Trace: " + ex.StackTrace, "Export error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
//github.com/ <summary>
//github.com/ Creates the OlapReport.
//github.com/ </summary>
//github.com/ <returns></returns>
OlapReport LoadReports()
{
OlapReport olapReport = new OlapReport();
olapReport.CurrentCubeName = "Adventure Works";
DimensionElement dimensionElementColumn = new DimensionElement();
// Specifying the Name for the Dimension Element
dimensionElementColumn.Name = "Customer";
dimensionElementColumn.AddLevel("Customer Geography", "Country");
MeasureElements measureElementColumn = new MeasureElements();
measureElementColumn.Elements.Add(new MeasureElement { Name = "Internet Sales Amount" });
DimensionElement dimensionElementRow = new DimensionElement();
// Specifying the Dimension Name
dimensionElementRow.Name = "Date";
dimensionElementRow.AddLevel("Fiscal", "Fiscal Year");
DimensionElement dimensionElementRow1 = new DimensionElement();
// Specifying the Dimension Name
dimensionElementRow1.Name = "Product";
dimensionElementRow1.AddLevel("Product Categories", "Category");
// Adding Column Members
olapReport.CategoricalElements.Add(dimensionElementColumn);
// Adding Measure Element
olapReport.CategoricalElements.Add(measureElementColumn);
// Adding Row Members
olapReport.SeriesElements.Add(dimensionElementRow);
olapReport.SeriesElements.Add(dimensionElementRow1);
return olapReport;
}
#endregion
}
}