1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | * ******************************************************************************
|
---|
29 | * Jan Källman Initial Release 2010-03-14
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using System.Security.Cryptography;
|
---|
37 | using OfficeOpenXml.Utils;
|
---|
38 | using OfficeOpenXml.Encryption;
|
---|
39 |
|
---|
40 | namespace OfficeOpenXml
|
---|
41 | {
|
---|
42 | /// <summary>
|
---|
43 | /// Sheet protection
|
---|
44 | ///<seealso cref="ExcelEncryption"/>
|
---|
45 | ///<seealso cref="ExcelProtection"/>
|
---|
46 | /// </summary>
|
---|
47 | public sealed class ExcelSheetProtection : XmlHelper
|
---|
48 | {
|
---|
49 | internal ExcelSheetProtection (XmlNamespaceManager nsm, XmlNode topNode,ExcelWorksheet ws) :
|
---|
50 | base(nsm, topNode)
|
---|
51 | {
|
---|
52 | SchemaNodeOrder = ws.SchemaNodeOrder;
|
---|
53 | }
|
---|
54 | private const string _isProtectedPath="d:sheetProtection/@sheet";
|
---|
55 | /// <summary>
|
---|
56 | /// If the worksheet is protected.
|
---|
57 | /// </summary>
|
---|
58 | public bool IsProtected
|
---|
59 | {
|
---|
60 | get
|
---|
61 | {
|
---|
62 | return GetXmlNodeBool(_isProtectedPath, false);
|
---|
63 | }
|
---|
64 | set
|
---|
65 | {
|
---|
66 | SetXmlNodeBool(_isProtectedPath, value, false);
|
---|
67 | if (value)
|
---|
68 | {
|
---|
69 | AllowEditObject = true;
|
---|
70 | AllowEditScenarios = true;
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | DeleteAllNode(_isProtectedPath); //delete the whole sheetprotection node
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | private const string _allowSelectLockedCellsPath = "d:sheetProtection/@selectLockedCells";
|
---|
79 | /// <summary>
|
---|
80 | /// Allow users to select locked cells
|
---|
81 | /// </summary>
|
---|
82 | public bool AllowSelectLockedCells
|
---|
83 | {
|
---|
84 | get
|
---|
85 | {
|
---|
86 | return !GetXmlNodeBool(_allowSelectLockedCellsPath, false);
|
---|
87 | }
|
---|
88 | set
|
---|
89 | {
|
---|
90 | SetXmlNodeBool(_allowSelectLockedCellsPath, !value, false);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | private const string _allowSelectUnlockedCellsPath = "d:sheetProtection/@selectUnlockedCells";
|
---|
94 | /// <summary>
|
---|
95 | /// Allow users to select unlocked cells
|
---|
96 | /// </summary>
|
---|
97 | public bool AllowSelectUnlockedCells
|
---|
98 | {
|
---|
99 | get
|
---|
100 | {
|
---|
101 | return !GetXmlNodeBool(_allowSelectUnlockedCellsPath, false);
|
---|
102 | }
|
---|
103 | set
|
---|
104 | {
|
---|
105 | SetXmlNodeBool(_allowSelectUnlockedCellsPath, !value, false);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | private const string _allowObjectPath="d:sheetProtection/@objects";
|
---|
109 | /// <summary>
|
---|
110 | /// Allow users to edit objects
|
---|
111 | /// </summary>
|
---|
112 | public bool AllowEditObject
|
---|
113 | {
|
---|
114 | get
|
---|
115 | {
|
---|
116 | return !GetXmlNodeBool(_allowObjectPath, false);
|
---|
117 | }
|
---|
118 | set
|
---|
119 | {
|
---|
120 | SetXmlNodeBool(_allowObjectPath, !value, false);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | private const string _allowScenariosPath="d:sheetProtection/@scenarios";
|
---|
124 | /// <summary>
|
---|
125 | /// Allow users to edit senarios
|
---|
126 | /// </summary>
|
---|
127 | public bool AllowEditScenarios
|
---|
128 | {
|
---|
129 | get
|
---|
130 | {
|
---|
131 | return !GetXmlNodeBool(_allowScenariosPath, false);
|
---|
132 | }
|
---|
133 | set
|
---|
134 | {
|
---|
135 | SetXmlNodeBool(_allowScenariosPath, !value, false);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | private const string _allowFormatCellsPath="d:sheetProtection/@formatCells";
|
---|
139 | /// <summary>
|
---|
140 | /// Allow users to format cells
|
---|
141 | /// </summary>
|
---|
142 | public bool AllowFormatCells
|
---|
143 | {
|
---|
144 | get
|
---|
145 | {
|
---|
146 | return !GetXmlNodeBool(_allowFormatCellsPath, true);
|
---|
147 | }
|
---|
148 | set
|
---|
149 | {
|
---|
150 | SetXmlNodeBool(_allowFormatCellsPath, !value, true );
|
---|
151 | }
|
---|
152 | }
|
---|
153 | private const string _allowFormatColumnsPath = "d:sheetProtection/@formatColumns";
|
---|
154 | /// <summary>
|
---|
155 | /// Allow users to Format columns
|
---|
156 | /// </summary>
|
---|
157 | public bool AllowFormatColumns
|
---|
158 | {
|
---|
159 | get
|
---|
160 | {
|
---|
161 | return !GetXmlNodeBool(_allowFormatColumnsPath, true);
|
---|
162 | }
|
---|
163 | set
|
---|
164 | {
|
---|
165 | SetXmlNodeBool(_allowFormatColumnsPath, !value, true);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | private const string _allowFormatRowsPath = "d:sheetProtection/@formatRows";
|
---|
169 | /// <summary>
|
---|
170 | /// Allow users to Format rows
|
---|
171 | /// </summary>
|
---|
172 | public bool AllowFormatRows
|
---|
173 | {
|
---|
174 | get
|
---|
175 | {
|
---|
176 | return !GetXmlNodeBool(_allowFormatRowsPath, true);
|
---|
177 | }
|
---|
178 | set
|
---|
179 | {
|
---|
180 | SetXmlNodeBool(_allowFormatRowsPath, !value, true);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private const string _allowInsertColumnsPath = "d:sheetProtection/@insertColumns";
|
---|
185 | /// <summary>
|
---|
186 | /// Allow users to insert columns
|
---|
187 | /// </summary>
|
---|
188 | public bool AllowInsertColumns
|
---|
189 | {
|
---|
190 | get
|
---|
191 | {
|
---|
192 | return !GetXmlNodeBool(_allowInsertColumnsPath, true);
|
---|
193 | }
|
---|
194 | set
|
---|
195 | {
|
---|
196 | SetXmlNodeBool(_allowInsertColumnsPath, !value, true);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | private const string _allowInsertRowsPath = "d:sheetProtection/@insertRows";
|
---|
201 | /// <summary>
|
---|
202 | /// Allow users to Format rows
|
---|
203 | /// </summary>
|
---|
204 | public bool AllowInsertRows
|
---|
205 | {
|
---|
206 | get
|
---|
207 | {
|
---|
208 | return !GetXmlNodeBool(_allowInsertRowsPath, true);
|
---|
209 | }
|
---|
210 | set
|
---|
211 | {
|
---|
212 | SetXmlNodeBool(_allowInsertRowsPath, !value, true);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | private const string _allowInsertHyperlinksPath = "d:sheetProtection/@insertHyperlinks";
|
---|
216 | /// <summary>
|
---|
217 | /// Allow users to insert hyperlinks
|
---|
218 | /// </summary>
|
---|
219 | public bool AllowInsertHyperlinks
|
---|
220 | {
|
---|
221 | get
|
---|
222 | {
|
---|
223 | return !GetXmlNodeBool(_allowInsertHyperlinksPath, true);
|
---|
224 | }
|
---|
225 | set
|
---|
226 | {
|
---|
227 | SetXmlNodeBool(_allowInsertHyperlinksPath, !value, true);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | private const string _allowDeleteColumns = "d:sheetProtection/@deleteColumns";
|
---|
231 | /// <summary>
|
---|
232 | /// Allow users to delete columns
|
---|
233 | /// </summary>
|
---|
234 | public bool AllowDeleteColumns
|
---|
235 | {
|
---|
236 | get
|
---|
237 | {
|
---|
238 | return !GetXmlNodeBool(_allowDeleteColumns, true);
|
---|
239 | }
|
---|
240 | set
|
---|
241 | {
|
---|
242 | SetXmlNodeBool(_allowDeleteColumns, !value, true);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | private const string _allowDeleteRowsPath = "d:sheetProtection/@deleteRows";
|
---|
246 | /// <summary>
|
---|
247 | /// Allow users to delete rows
|
---|
248 | /// </summary>
|
---|
249 | public bool AllowDeleteRows
|
---|
250 | {
|
---|
251 | get
|
---|
252 | {
|
---|
253 | return !GetXmlNodeBool(_allowDeleteRowsPath, true);
|
---|
254 | }
|
---|
255 | set
|
---|
256 | {
|
---|
257 | SetXmlNodeBool(_allowDeleteRowsPath, !value, true);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | private const string _allowSortPath = "d:sheetProtection/@sort";
|
---|
262 | /// <summary>
|
---|
263 | /// Allow users to sort a range
|
---|
264 | /// </summary>
|
---|
265 | public bool AllowSort
|
---|
266 | {
|
---|
267 | get
|
---|
268 | {
|
---|
269 | return !GetXmlNodeBool(_allowSortPath, true);
|
---|
270 | }
|
---|
271 | set
|
---|
272 | {
|
---|
273 | SetXmlNodeBool(_allowSortPath, !value, true);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | private const string _allowAutoFilterPath = "d:sheetProtection/@autoFilter";
|
---|
278 | /// <summary>
|
---|
279 | /// Allow users to use autofilters
|
---|
280 | /// </summary>
|
---|
281 | public bool AllowAutoFilter
|
---|
282 | {
|
---|
283 | get
|
---|
284 | {
|
---|
285 | return !GetXmlNodeBool(_allowAutoFilterPath, true);
|
---|
286 | }
|
---|
287 | set
|
---|
288 | {
|
---|
289 | SetXmlNodeBool(_allowAutoFilterPath, !value, true);
|
---|
290 | }
|
---|
291 | }
|
---|
292 | private const string _allowPivotTablesPath = "d:sheetProtection/@pivotTables";
|
---|
293 | /// <summary>
|
---|
294 | /// Allow users to use pivottables
|
---|
295 | /// </summary>
|
---|
296 | public bool AllowPivotTables
|
---|
297 | {
|
---|
298 | get
|
---|
299 | {
|
---|
300 | return !GetXmlNodeBool(_allowPivotTablesPath, true);
|
---|
301 | }
|
---|
302 | set
|
---|
303 | {
|
---|
304 | SetXmlNodeBool(_allowPivotTablesPath, !value, true);
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | private const string _passwordPath = "d:sheetProtection/@password";
|
---|
309 | /// <summary>
|
---|
310 | /// Sets a password for the sheet.
|
---|
311 | /// </summary>
|
---|
312 | /// <param name="Password"></param>
|
---|
313 | public void SetPassword(string Password)
|
---|
314 | {
|
---|
315 | if (IsProtected == false) IsProtected = true;
|
---|
316 |
|
---|
317 | Password = Password.Trim();
|
---|
318 | if (Password == "")
|
---|
319 | {
|
---|
320 | var node = TopNode.SelectSingleNode(_passwordPath, NameSpaceManager);
|
---|
321 | if (node != null)
|
---|
322 | {
|
---|
323 | (node as XmlAttribute).OwnerElement.Attributes.Remove(node as XmlAttribute);
|
---|
324 | }
|
---|
325 | return;
|
---|
326 | }
|
---|
327 |
|
---|
328 | int hash = EncryptedPackageHandler.CalculatePasswordHash(Password);
|
---|
329 | SetXmlNodeString(_passwordPath, ((int)hash).ToString("x"));
|
---|
330 | }
|
---|
331 |
|
---|
332 | }
|
---|
333 | }
|
---|