-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.vb
More file actions
82 lines (71 loc) · 3.61 KB
/
Program.vb
File metadata and controls
82 lines (71 loc) · 3.61 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
Imports System
Imports System.Diagnostics
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 Workbook = 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 EncryptionSettings = 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)
Call 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 Equals(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 Equals(answer, "y") Then
e.TryAgain = True
e.Handled = True
Else
IsValid = False
End If
End Select
IsValid = False
End Sub
End Class
End Namespace