-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
108 lines (93 loc) · 3.77 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
namespace EncryptionExample
{
class Program
{
static bool IsValid { get; set; }
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.Options.Import.Password = "123";
workbook.LoadDocument("Documents\\encrypted.xlsx");
workbook.EncryptedFilePasswordRequest += Workbook_EncryptedFilePasswordRequest;
workbook.EncryptedFilePasswordCheckFailed += Workbook_EncryptedFilePasswordCheckFailed;
workbook.InvalidFormatException += Workbook_InvalidFormatException;
EncryptionSettings encryptionOptions = new EncryptionSettings();
encryptionOptions.Type = EncryptionType.Strong;
encryptionOptions.Password = "12345";
Console.WriteLine("Select the file format: XLSX/XLS/XLSB");
string answerFormat = Console.ReadLine()?.ToLower();
DocumentFormat documentFormat = DocumentFormat.Undefined;
switch (answerFormat)
{
case "xlsx":
documentFormat = DocumentFormat.OpenXml;
break;
case "xls":
documentFormat = DocumentFormat.Xls;
break;
case "xlsb":
documentFormat = DocumentFormat.Xlsb;
break;
}
string fileName = String.Format("EncryptedwithNewPassword.{0}", answerFormat);
workbook.SaveDocument(fileName, documentFormat, encryptionOptions);
if (IsValid == true)
{
workbook.SaveDocument(fileName, documentFormat);
Process.Start(fileName);
}
Console.WriteLine("The document is saved with new password. Continue? (y/n)");
string answer = Console.ReadLine()?.ToLower();
if (answer == "y")
{
Console.WriteLine("Re-opening the file...");
workbook.LoadDocument(fileName);
}
}
private static void Workbook_InvalidFormatException(object sender, SpreadsheetInvalidFormatExceptionEventArgs e)
{
Console.WriteLine(e.Exception.Message.ToString() + " Press any key to close...");
Console.ReadKey(true);
}
private static void Workbook_EncryptedFilePasswordRequest(object sender, EncryptedFilePasswordRequestEventArgs e)
{
Console.WriteLine("Enter password:");
e.Password = Console.ReadLine();
e.Handled = true;
IsValid = true;
}
private static void Workbook_EncryptedFilePasswordCheckFailed(object sender, EncryptedFilePasswordCheckFailedEventArgs e)
{
switch (e.Error)
{
case SpreadsheetDecryptionError.PasswordRequired:
Console.WriteLine("You did not enter the password!");
e.TryAgain = true;
e.Handled = true;
break;
case SpreadsheetDecryptionError.WrongPassword:
Console.WriteLine("The password is incorrect. Try Again? (y/n)");
string answer = Console.ReadLine()?.ToLower();
if (answer == "y")
{
e.TryAgain = true;
e.Handled = true;
}
else
{
IsValid = false;
}
break;
}
Program.IsValid = false;
}
}
}