-
Notifications
You must be signed in to change notification settings - Fork 103
/
View.ascx.cs
452 lines (398 loc) · 21.1 KB
/
View.ascx.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
' Copyright (c) 2015 Aspose.com
' All rights reserved.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
*/
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Aspose.Cells;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace Aspose.DotNetNuke.Modules.AsposeDynamicFormGeneratorExcel
{
/// -----------------------------------------------------------------------------
/// <summary>
/// The View class displays the content
///
/// Typically your view control would be used to display content or functionality in your module.
///
/// View may be the only control you have in your project depending on the complexity of your module
///
/// Because the control inherits from AsposeDNNDynamicFormGeneratorModuleBase you have access to any custom properties
/// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
///
/// </summary>
/// -----------------------------------------------------------------------------
public partial class View : AsposeDNNDynamicFormGeneratorModuleBase, IActionable
{
#region "Event Handlers"
//Default page load event
protected void Page_Load(object sender, EventArgs e)
{
try
{
// Load form view and data source Table, Views dropdownns
InitForm();
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
// Save data to excel file
protected void ProcessButton_Click(object sender, EventArgs e)
{
try
{
// Check for license and apply if exists
string licenseFile = Server.MapPath("~/App_Data/Aspose.Total.lic");
if (File.Exists(licenseFile))
{
License license = new License();
license.SetLicense(licenseFile);
}
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(Server.MapPath("~/DesktopModules/Aspose.DNN.DynamicFormGenerator.Excel/Docs/AsposeDynamicFormsDataFile.xlsx"), FileMode.Open, FileAccess.Read);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Data"];
Worksheet worksheet1 = workbook.Worksheets["Settings"];
//Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
DataTable dataTable = null;
if (worksheet.Cells.Rows.Count <= 0)
{
dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, 1, 1, true);
}
else
{
dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.Rows.Count, worksheet.Cells.Columns.Count, true);
}
//Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
DataTable dataTable1 = worksheet1.Cells.ExportDataTableAsString(0, 0, worksheet1.Cells.Rows.Count, 10, true);
if (dataTable1 != null)
{
if (dataTable != null)
{
if (dataTable.Columns.Count <= 1)
{
dataTable.Columns.RemoveAt(0);
}
foreach (DataRow row in dataTable1.Rows)
{
if (!row[1].ToString().Trim().Equals("Title") && !row[1].ToString().Trim().Equals("Success"))
{
foreach (string strItem in row[2].ToString().Trim().Split(';'))
{
if (!strItem.Trim().Equals(""))
{
if (dataTable.Columns[strItem.Trim()] == null)
{
dataTable.Columns.Add(strItem.Trim());
}
}
}
}
}
}
}
//Closing the file stream to free all resources
fstream.Close();
DataRow dr = dataTable.NewRow();
foreach (Control ctrl in myPlaceHolder.Controls)
{
if (ctrl != null)
{
if (ctrl is TextBox)
{
dr[ctrl.ID] = ((TextBox)ctrl).Text.Trim();
continue;
}
if (ctrl is RadioButton)
{
if (((RadioButton)ctrl).Checked)
{
dr[ctrl.ID] = ((RadioButton)ctrl).Text.Trim();
continue;
}
}
if (ctrl is CheckBox)
{
if (((CheckBox)ctrl).Checked)
{
dr[ctrl.ID] = ((CheckBox)ctrl).Text.Trim();
continue;
}
}
if (ctrl is DropDownList)
{
dr[ctrl.ID] = ((DropDownList)ctrl).SelectedItem.Text.Trim();
continue;
}
}
}
dataTable.Rows.Add(dr);
workbook.Worksheets.RemoveAt("Data");
worksheet = workbook.Worksheets.Add("Data");
worksheet.Cells.ImportDataTable(dataTable, true, "A1");
// Apply Hearder Row/First Row text to Bold
Cells.Style objStyle = new Cells.Style();
objStyle.Font.IsBold = true;
StyleFlag objStyleFlag = new StyleFlag();
objStyleFlag.FontBold = true;
worksheet.Cells.ApplyRowStyle(0, objStyle, objStyleFlag);
//Auto-fit all the columns
workbook.Worksheets["Data"].AutoFitColumns();
workbook.Save(Server.MapPath("~/DesktopModules/Aspose.DNN.DynamicFormGenerator.Excel/Docs/AsposeDynamicFormsDataFile.xlsx"), SaveFormat.Xlsx);
error_msg.Visible = false;
success_msg.Visible = true;
}
catch (Exception exc)
{
success_msg.Visible = false;
error_msg.Visible = true;
error_msg.InnerText = exc.Message;
}
}
// Save data to excel file
protected void ClearFormButton_Click(object sender, EventArgs e)
{
try
{
Response.Redirect(Request.Url.ToString());
}
catch (Exception exc)
{
success_msg.Visible = false;
error_msg.Visible = true;
error_msg.InnerText = exc.Message;
}
}
//Module action, on edit
public ModuleActionCollection ModuleActions
{
get
{
var actions = new ModuleActionCollection
{
{
GetNextActionID(), "Export using Aspose.Cells", "", "", "/DesktopModules/Aspose.DNN.DynamicFormGenerator.Excel/Images/aspose_logo.ico",
EditUrl(), false, SecurityAccessLevel.Edit, true, false
}
};
return actions;
}
}
#endregion
#region "Private Functions"
//Form Initiating by creating dynamic controls from Excel sheet
private void InitForm()
{
try
{
// Check for license and apply if exists
string licenseFile = Server.MapPath("~/App_Data/Aspose.Total.lic");
if (File.Exists(licenseFile))
{
License license = new License();
license.SetLicense(licenseFile);
}
DataTable dataTable = null;
if (Session["AsposeDynamicFormsdataTable"] == null)
{
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(Server.MapPath("~/DesktopModules/Aspose.DNN.DynamicFormGenerator.Excel/Docs/AsposeDynamicFormsDataFile.xlsx"), FileMode.Open, FileAccess.Read);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Settings"];
//Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.Rows.Count, 10, true);
//Closing the file stream to free all resources
fstream.Close();
dataTable.DefaultView.Sort = "[Sort ID] ASC";
dataTable = dataTable.DefaultView.ToTable();
Session["AsposeDynamicFormsdataTable"] = dataTable;
}
else
{
dataTable = (DataTable)Session["AsposeDynamicFormsdataTable"];
}
if (dataTable != null)
{
if (dataTable.Rows.Count > 0)
{
string fieldType = "Text";
int rows = 0;
foreach (DataRow row in dataTable.Rows)
{
if (!row[7].ToString().Trim().ToLower().Equals("false"))
{
rows += 1;
fieldType = row[1].ToString();
switch (fieldType)
{
case "Title":
{
lblTitle.Text = row[4].ToString().Trim();
}
break;
case "Success":
{
success_msg.InnerText = row[4].ToString().Trim();
}
break;
case "Text":
{
Label lbl = new Label();
lbl.ID = "lbl" + row[2].ToString();
lbl.Text = row[3].ToString();
lbl.CssClass = "dnnLabel";
myPlaceHolder.Controls.Add(lbl);
TextBox textBox = new TextBox();
textBox.ID = row[2].ToString().Trim();
textBox.Attributes.Add("placeholder", row[4].ToString().Trim());
if (myPlaceHolder.FindControl(textBox.ID) == null)
myPlaceHolder.Controls.Add(textBox);
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
break;
case "MultiText":
{
Label lbl = new Label();
lbl.ID = "lbl" + row[2].ToString().Trim();
lbl.Text = row[3].ToString().Trim();
lbl.CssClass = "dnnLabel";
myPlaceHolder.Controls.Add(lbl);
TextBox textBox = new TextBox();
textBox.ID = row[2].ToString().Trim();
textBox.Attributes.Add("placeholder", row[4].ToString().Trim());
textBox.TextMode = TextBoxMode.MultiLine;
textBox.Rows = 5;
if (myPlaceHolder.FindControl(textBox.ID) == null)
myPlaceHolder.Controls.Add(textBox);
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
break;
case "Radio":
{
Label lbl = new Label();
lbl.ID = "lbl" + row[2].ToString().Trim();
lbl.Text = row[3].ToString().Trim();
lbl.CssClass = "dnnLabel";
myPlaceHolder.Controls.Add(lbl);
int i = 0;
foreach (string strItem in row[2].ToString().Trim().Split(';'))
{
if (!strItem.Equals(""))
{
RadioButton radioButton = new RadioButton();
radioButton.GroupName = "RadioGroup" + rows.ToString();
if (row[4].ToString().Trim().Split(';').Length >= i)
{
radioButton.Text = row[4].ToString().Trim().Split(';')[i];
radioButton.ID = row[2].ToString().Trim().Split(';')[i];
if (i == 0)
{
radioButton.Checked = true;
}
}
if (myPlaceHolder.FindControl(radioButton.ID) == null)
myPlaceHolder.Controls.Add(radioButton);
i++;
}
}
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
break;
case "Check":
{
Label lbl = new Label();
lbl.ID = "lbl" + row[2].ToString().Trim();
lbl.Text = row[3].ToString().Trim();
lbl.CssClass = "dnnLabel";
myPlaceHolder.Controls.Add(lbl);
int i = 0;
foreach (string strItem in row[4].ToString().Trim().Split(';'))
{
if (!strItem.Equals(""))
{
CheckBox checkBox = new CheckBox();
if (row[4].ToString().Trim().Split(';').Length >= i)
{
checkBox.Text = row[4].ToString().Trim().Split(';')[i];
checkBox.ID = row[2].ToString().Trim().Split(';')[i];
}
if (myPlaceHolder.FindControl(checkBox.ID) == null)
myPlaceHolder.Controls.Add(checkBox);
i++;
}
}
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
break;
case "DropDown":
{
Label lbl = new Label();
lbl.ID = "lbl" + row[2].ToString().Trim();
lbl.Text = row[3].ToString().Trim();
lbl.CssClass = "dnnLabel";
myPlaceHolder.Controls.Add(lbl);
DropDownList dropdownList = new DropDownList();
dropdownList.ID = row[2].ToString().Trim();
foreach (string strItem in row[4].ToString().Trim().Split(';'))
{
if (!strItem.Equals(""))
{
dropdownList.Items.Add(strItem);
}
}
if (myPlaceHolder.FindControl(dropdownList.ID) == null)
myPlaceHolder.Controls.Add(dropdownList);
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
break;
default:
break;
}
}
}
}
}
}
catch (Exception exc)
{
success_msg.Visible = false;
error_msg.Visible = true;
error_msg.InnerText = exc.Message;
}
}
#endregion
}
}