-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathExportToExcel.aspx.cs
More file actions
171 lines (147 loc) · 6.54 KB
/
ExportToExcel.aspx.cs
File metadata and controls
171 lines (147 loc) · 6.54 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using Aspose.Cells;
using System.Drawing;
using System.IO;
namespace Aspose.UmbracoMemberExportToExcel
{
public partial class AsposeMemberExport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
ErrorLabel.Visible = false;
if (!Page.IsPostBack)
LoadMembers();
}
catch (Exception ex)
{
ErrorLabel.Text = ex.ToString();
ErrorLabel.Visible = true;
}
}
void LoadMembers()
{
IEnumerable<Member> listMembers = Member.GetAllAsList();
UmbracoMembersGridView.DataSource = listMembers;
UmbracoMembersGridView.DataBind();
if (UmbracoMembersGridView.Rows.Count > 0)
{
UmbracoMembersGridView.UseAccessibleHeader = true;
UmbracoMembersGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
protected void ExportButton_Click(object sender, EventArgs e)
{
try
{
// Check for license and apply if exists
string licenseFile = Server.MapPath("~/App_Data/Aspose.Cells.lic");
if (File.Exists(licenseFile))
{
License license = new License();
license.SetLicense(licenseFile);
}
List<Member> selectedMembersList = new List<Member>();
foreach (GridViewRow row in UmbracoMembersGridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("SelectedCheckBox") as CheckBox);
if (chkRow.Checked)
{
string email = UmbracoMembersGridView.DataKeys[row.RowIndex].Value.ToString();
selectedMembersList.Add(Member.GetMemberFromEmail(email));
}
}
}
if (selectedMembersList.Count() < 1)
{
selectedMembersList = Member.GetAllAsList().ToList<Member>();
}
//Instantiate a new Workbook
Workbook book = new Workbook();
//Clear all the worksheets
book.Worksheets.Clear();
//Add a new Sheet "Data";
Worksheet sheet = book.Worksheets.Add("MembersExport");
//We pick a few columns not all to import to the worksheet
sheet.Cells.ImportCustomObjects((System.Collections.ICollection)selectedMembersList,
new string[] { "Text", "LoginName", "Email", "CreateDateTime" }, true, 0, 0, selectedMembersList.Count(), true, "dd/mm/yyyy", false);
Worksheet worksheet = book.Worksheets[0];
ApplyCellStyle(ref worksheet, "A1");
worksheet.Cells["A1"].PutValue("Name");
ApplyCellStyle(ref worksheet, "B1");
ApplyCellStyle(ref worksheet, "C1");
ApplyCellStyle(ref worksheet, "D1");
worksheet.Cells.InsertRow(0);
worksheet.Cells.InsertColumn(0);
worksheet.AutoFitColumns();
string fileName = System.Guid.NewGuid().ToString() + "." + ExportTypeDropDown.SelectedValue;
if (SaveCopyToDiskCheckBox.Checked)
{
string exportDirectory = Server.MapPath("~/App_Data/AsposeMemberExport");
if (!Directory.Exists(exportDirectory)) Directory.CreateDirectory(exportDirectory);
book.Save(exportDirectory + "/" + fileName);
}
book.Save(this.Response, fileName, ContentDisposition.Attachment, GetSaveFormat(ExportTypeDropDown.SelectedValue));
Response.End();
}
catch (Exception ex)
{
ErrorLabel.Text = ex.ToString();
ErrorLabel.Visible = true;
}
}
private XlsSaveOptions GetSaveFormat(string format)
{
XlsSaveOptions saveOption = new XlsSaveOptions(SaveFormat.Xlsx);
switch (format)
{
case "xlsx":
saveOption = new XlsSaveOptions(SaveFormat.Xlsx); break;
case "xlsb":
saveOption = new XlsSaveOptions(SaveFormat.Xlsb); break;
case "xls":
saveOption = new XlsSaveOptions(SaveFormat.Excel97To2003); break;
case "txt":
saveOption = new XlsSaveOptions(SaveFormat.TabDelimited); break;
case "csv":
saveOption = new XlsSaveOptions(SaveFormat.CSV); break;
case "ods":
saveOption = new XlsSaveOptions(SaveFormat.ODS); break;
}
return saveOption;
}
private void ApplyCellStyle(ref Worksheet worksheet, string cellName)
{
Cell cell = worksheet.Cells[cellName];
Aspose.Cells.Style style = cell.GetStyle();
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].Color = Color.Black;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].Color = Color.Black;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].Color = Color.Black;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].Color = Color.Black;
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = Color.LightGray;
style.Font.IsBold = true;
cell.SetStyle(style);
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (ErrorLabel.Visible)
{
ErrorLabel.Text = "<br>" + ErrorLabel.Text + "<br>";
}
}
}
}