基本上,我想开发一个office excel插件。安装此插件后,创建的所有 excel 文件只能由其 excel 安装了此插件的用户打开。所以我在这里有两个问题:
最佳答案
美丽的问题:)
是的,它可以在 VBA Excel 插件中实现。事实上,几年前我确实实现了类似的东西。以下是分 4 步解释的逻辑。
通过加载项创建文件时,请按照下列步骤操作
1) 将文件保存在 Temp directory
比如说Temp.xls
和密码 用密码保护它。您可以选择保留 固定密码或 动态一。请参阅此代码以创建动态密码。
Function GenerateRandPassword() As String
Dim strEnvPass As String, strPass As String
Dim n As Integer
strEnvPass = "@#$%^"
n = Application.WorksheetFunction.RandBetween(1, 10)
For i = 1 To 5
strPass = strPass & n
Next i
strPass = strEnvPass & strPass & strEnvPass
GenerateRandPassword = strPass
End Function
要测试它,只需像这样调用它
Sub Sample()
Debug.Print GenerateRandPassword
End Sub
密码将采用这种格式
@#$%^11111@#$%^
@#$%^22222@#$%^
@#$%^33333@#$%^
.
.
.
@#$%^1010101010@#$%^
当您尝试通过加载项打开文件时,循环 1 到 10 并使用
@#$%^
作为后缀和前缀来创建密码,然后尝试打开文件。这样你的 Excel 文件就会有随 secret 码。有关如何打开此类文件,请参阅第 4 点。import/export 商 : 不要在
Application.WorksheetFunction.RandBetween(1, 10)
中选择太大的数字它只会延迟稍后打开文件。2) 要获取临时目录,请使用此代码
Private Declare Function GetTempPath _
Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function
Sub TmpPath()
'This will give the Temp Path
MsgBox TempPath
End Sub
3) 在临时目录中创建文件后,重命名文件并为其指定 Excel 无法识别的扩展名,例如
MyFile.ice
(从您的 SO 句柄中取出前 3 个字母 iceagle
)您可以使用此代码重命名文件
Name TempPath & "MyFile.xls" As "C:\MyFile.Ice"
这样,普通用户将不知道打开此文件需要哪个应用程序。
要打开此文件,您可以将过滤器设置为
*.ice
在 Application.GetOpenFilenam
Sub Sample()
fileToOpen = Application.GetOpenFilename("ICE Files (*.ice), *.ice")
If fileToOpen <> False Then
Application.DisplayAlerts = False
Workbooks.Open (fileToOpen)
Application.DisplayAlerts = True
End If
End Sub
4) 要打开文件,请使用此代码
Sub OpenFile()
Dim wb As Workbook
Dim strPass As String
For i = 1 To 10
On Error Resume Next
strPass = "@#$%^" & i & i & i & i & i & "@#$%^"
Set wb = Workbooks.Open("C:\MyFile.ice", , , , strPass)
If Err.Number = 0 Then Exit For
Err.Clear
Next
On Error GoTo 0
End Sub
希望这能让你开始:)
关于c# - 如何保护我的excel文件不被其他人阅读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192641/