使用c#更改excel密码?
我正在开发这个项目,该项目使用 Excel.interop 创建工作表,目前它们受密码保护。使用 C# 修改密码的最佳方法是什么?我是互操作新手,所以不熟悉所有可用的功能。
目前我使用 worksheet.unprotect(oldpassword) 使用旧密码解锁工作表,然后调用 worksheet.protect(newpassword) 使用新密码将其锁定。但是随后出现了这个问题。它第一次工作正常,但之后当它尝试使用 oldpw 取消保护时,我遇到异常。所以旧的密码是 1 次使用,我如何在 C# 中实现该逻辑?另外,我有大约 15 个工作表(全部受密码保护),因此使用计数器时会变得很复杂。
目前我使用这样的 try catch 块。
public static void Unprotect_Worksheet(string Name)
{
try
{
//try old pw first, if gets exception, retry using new pw
string strPassword = oldpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];
wsheet.Unprotect(oldpassword);
}
catch
{
string strPassword = newpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];
wsheet.Unprotect(strPassword);
}
}
这工作得很好,但我真的不认为 catch 块应该用于实现业务逻辑。有更好的方法来解决这个问题吗?
也许我可以从 catch 块内部返回 0 来调用方法,然后调用不同的方法来使用新密码取消保护工作表。但这将是代码复制。有什么专业知识吗???
I am working on this project which uses Excel.interop to create worksheets and currently they are password protected.Whats the best way to modify the password using c#? I am new to interop, so not familiar with all the functionalities available.
Currently I use the worksheet.unprotect(oldpassword) to unlock the sheet using old password and then call worksheet.protect(newpassword) to lock it back using new password.But then arises this problem. It works fine for the first time, but after that when it tries to unprotect using oldpw, I get exception. So the old pw is a 1 time use, how do i implement that logic in c#? Also I have like 15 worksheets (all password protected) and so it gets complicated when using counters.
Currently I use a try catch block like this.
public static void Unprotect_Worksheet(string Name)
{
try
{
//try old pw first, if gets exception, retry using new pw
string strPassword = oldpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];
wsheet.Unprotect(oldpassword);
}
catch
{
string strPassword = newpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];
wsheet.Unprotect(strPassword);
}
}
This works perfectly fine, but I don't really think a catch block should be used for implementing business logic. is there a better way to solve this?
May be I can return 0 to calling method from inside catch block, and then call a different method to unprotect worksheet using new password. But that would be code replication. Any expertise???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:我不进行 Office 互操作编程。话虽这么说,我做了很多其他编程,所以也许这会是有用的建议。
首先,我在上面的评论中向 Tim Williams 表示支持,建议您在更熟悉代码后重新访问它。在您花一些时间研究现有代码之前,重新编写逻辑是不明智的。
现在:如果在您指定错误密码时对 Worksheet.Unprotect(这是一个 COM 接口)的调用产生异常,那么您将必须尝试/捕获该异常。这似乎是处理错误的唯一方法。因此,要回答您的一个问题,您需要 try/catch 块。
鉴于您提供的代码,我可能会像这样重新处理它:
Disclaimer: I don't do Office interop programming. That being said, I do a lot of other programming, so maybe this will be helpful advice.
First, I give props to Tim Williams in the comments above for suggesting you revisit it later once you're more familiar with the code. It's not wise to rework the logic until you've spent some time with the existing code.
Now: If the call to Worksheet.Unprotect (which is a COM interface) produces an exception when you specify the wrong password, then you're going to have to try/catch the exception. That appears to be the only way to handle the error. So, to answer one question of yours, you need that try/catch block.
Given the code you provided, I might re-work it like this: