-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.vb
90 lines (75 loc) · 3.02 KB
/
Program.vb
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
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet
Namespace EncryptionExample
Friend Class Program
Private Shared Property IsValid() As Boolean
Shared Sub Main(ByVal args() As String)
Dim workbook As New Workbook()
workbook.Options.Import.Password = "123"
workbook.LoadDocument("Documents\encrypted.xlsx")
AddHandler workbook.EncryptedFilePasswordRequest, AddressOf Workbook_EncryptedFilePasswordRequest
AddHandler workbook.EncryptedFilePasswordCheckFailed, AddressOf Workbook_EncryptedFilePasswordCheckFailed
AddHandler workbook.InvalidFormatException, AddressOf Workbook_InvalidFormatException
Dim encryptionOptions As New EncryptionSettings()
encryptionOptions.Type = EncryptionType.Strong
encryptionOptions.Password = "12345"
Console.WriteLine("Select the file format: XLSX/XLS/XLSB")
Dim answerFormat As String = Console.ReadLine()?.ToLower()
Dim documentFormat As DocumentFormat = DocumentFormat.Undefined
Select Case answerFormat
Case "xlsx"
documentFormat = DocumentFormat.OpenXml
Case "xls"
documentFormat = DocumentFormat.Xls
Case "xlsb"
documentFormat = DocumentFormat.Xlsb
End Select
Dim fileName As String = String.Format("EncryptedwithNewPassword.{0}", answerFormat)
workbook.SaveDocument(fileName, documentFormat, encryptionOptions)
If IsValid = True Then
workbook.SaveDocument(fileName, documentFormat)
Process.Start(fileName)
End If
Console.WriteLine("The document is saved with new password. Continue? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If answer = "y" Then
Console.WriteLine("Re-opening the file...")
workbook.LoadDocument(fileName)
End If
End Sub
Private Shared Sub Workbook_InvalidFormatException(ByVal sender As Object, ByVal e As SpreadsheetInvalidFormatExceptionEventArgs)
Console.WriteLine(e.Exception.Message.ToString() & " Press any key to close...")
Console.ReadKey(True)
End Sub
Private Shared Sub Workbook_EncryptedFilePasswordRequest(ByVal sender As Object, ByVal e As EncryptedFilePasswordRequestEventArgs)
Console.WriteLine("Enter password:")
e.Password = Console.ReadLine()
e.Handled = True
IsValid = True
End Sub
Private Shared Sub Workbook_EncryptedFilePasswordCheckFailed(ByVal sender As Object, ByVal e As EncryptedFilePasswordCheckFailedEventArgs)
Select Case e.Error
Case SpreadsheetDecryptionError.PasswordRequired
Console.WriteLine("You did not enter the password!")
e.TryAgain = True
e.Handled = True
Case SpreadsheetDecryptionError.WrongPassword
Console.WriteLine("The password is incorrect. Try Again? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If answer = "y" Then
e.TryAgain = True
e.Handled = True
Else
IsValid = False
End If
End Select
Program.IsValid = False
End Sub
End Class
End Namespace