forked from aspose-words/Aspose.Words-for-.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsposeExportUsersToWord.ascx.cs
More file actions
187 lines (151 loc) · 7.29 KB
/
AsposeExportUsersToWord.ascx.cs
File metadata and controls
187 lines (151 loc) · 7.29 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;
using Aspose.Words;
using Aspose.Words.Saving;
using System.IO;
using System.Text;
namespace Aspose.Sitefinity.ExportUsersToWord
{
public partial class AsposeExportUsersToWord : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
UserManager userManager = UserManager.GetManager();
List<User> users = userManager.GetUsers().ToList();
DataTable output = new DataTable("ASA");
output.Columns.Add("LastName");
output.Columns.Add("FirstName");
output.Columns.Add("Email");
output.Columns.Add("Username");
output.Columns.Add("UserID");
foreach (User user in users)
{
UserProfileManager profileManager = UserProfileManager.GetManager();
SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);
DataRow dr;
dr = output.NewRow();
dr["FirstName"] = profile.FirstName;
dr["Username"] = user.UserName;
dr["Email"] = user.Email;
dr["LastName"] = profile.LastName;
dr["UserID"] = user.Id;
output.Rows.Add(dr);
}
Document d = new Document();
SitefinityUsersGridView.DataSource = output;
//if (!IsPostBack)
SitefinityUsersGridView.DataBind();
}
protected void ExportButton_Click(object sender, EventArgs e)
{
string format = ExportTypeDropDown.SelectedValue;
DataTable selectedUsers = new DataTable("SU");
selectedUsers.Columns.Add("UserID");
selectedUsers.Columns.Add("DisplayName");
selectedUsers.Columns.Add("Email");
selectedUsers.Columns.Add("Username");
foreach (GridViewRow row in SitefinityUsersGridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("SelectedCheckBox") as CheckBox);
if (chkRow.Checked)
{
string email = SitefinityUsersGridView.DataKeys[row.RowIndex].Value.ToString();
var userMan = UserManager.GetManager();
User user = userMan.GetUserByEmail(email);
UserProfileManager profileManager = UserProfileManager.GetManager();
SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);
DataRow dr;
dr = selectedUsers.NewRow();
dr["UserID"] = user.Id;
dr["DisplayName"] = profile.FirstName + " " + profile.LastName;
dr["Email"] = user.Email;
dr["Username"] = user.UserName;
selectedUsers.Rows.Add(dr);
}
}
}
if (selectedUsers.Rows.Count == 0)
{
NoRowSelectedErrorDiv.Visible = true;
}
else
{
// Check for license and apply if exists
string licenseFile = Server.MapPath("~/App_Data/Aspose.Words.lic");
if (File.Exists(licenseFile))
{
License license = new License();
license.SetLicense(licenseFile);
}
string content_header_rich = "<tr><th>UserID</th><th>Display Name</th><th>Email</th><th>Username</th></tr>";
string content_rows_rich = "";
foreach (DataRow user in selectedUsers.Rows)
{
content_rows_rich = string.Concat(content_rows_rich, "<tr><td>" + user["UserID"].ToString() + "</td><td>" + user["DisplayName"].ToString() + "</td><td>" + user["Email"].ToString() + "</td><td>" + user["Username"].ToString() + "</td></tr>");
}
string content_rich = "<html><h2>Exported Users</h2><table width=\"100%\" style='border: 1px solid black; border-collapse: collapse;' >" + content_header_rich + content_rows_rich + "</table></html>";
string fileName = System.Guid.NewGuid().ToString() + "." + format;
if (format != "txt")
{
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(content_rich));
Document doc = new Document(stream);
doc.Save(Response, fileName, ContentDisposition.Inline, null);
Response.End();
}
else
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string delim = "\t";
builder.Writeln("UserID" + delim + "DisplayName" + delim + "Email" + delim + "Username");
foreach (DataRow user in selectedUsers.Rows)
{
string row = user["UserID"].ToString() + delim + user["DisplayName"].ToString() + delim + user["Email"].ToString() + delim + user["Username"].ToString();
builder.Writeln(row);
}
doc.Save(this.Response, fileName, ContentDisposition.Attachment, GetSaveFormat(format));
Response.End();
}
}
}
private SaveOptions GetSaveFormat(string format)
{
SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Docx);
switch (format)
{
case "pdf":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Pdf); break;
case "doc":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Doc); break;
case "docx":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Docx); break;
case "dot":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Dot); break;
case "dotx":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Dotx); break;
case "docm":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Docm); break;
case "dotm":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Dotm); break;
case "odt":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Odt); break;
case "ott":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Ott); break;
case "rtf":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Rtf); break;
case "txt":
saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Text); break;
}
return saveOptions;
}
}
}