使用c#更改excel密码?

发布于 2024-12-13 15:38:46 字数 1176 浏览 3 评论 0原文

我正在开发这个项目,该项目使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-12-20 15:38:46

免责声明:我不进行 Office 互操作编程。话虽这么说,我做了很多其他编程,所以也许这会是有用的建议。

首先,我在上面的评论中向 Tim Williams 表示支持,建议您在更熟悉代码后重新访问它。在您花一些时间研究现有代码之前,重新编写逻辑是不明智的。

现在:如果在您指定错误密码时对 Worksheet.Unprotect(这是一个 COM 接口)的调用产生异常,那么您将必须尝试/捕获该异常。这似乎是处理错误的唯一方法。因此,要回答您的一个问题,您需要 try/catch 块。

鉴于您提供的代码,我可能会像这样重新处理它:

string strPassword = oldpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];

try { 
  // try the old password first (throws a COM exception if it fails)
  wsheet.Unprotect(oldpassword);
} catch {
  // ideally we should make sure we're only handling an invalid password error here
  try {
    // couldn't unprotect with the old password - try the new password
    strPassword = newpassword;
    wsheet.Unprotect(strPassword);
  } catch(Exception ex) {
    // TODO neither password worked - what do we do now? [insert code here...]
  }
}

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:

string strPassword = oldpassword;
Excel.Worksheet wsheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Name];

try { 
  // try the old password first (throws a COM exception if it fails)
  wsheet.Unprotect(oldpassword);
} catch {
  // ideally we should make sure we're only handling an invalid password error here
  try {
    // couldn't unprotect with the old password - try the new password
    strPassword = newpassword;
    wsheet.Unprotect(strPassword);
  } catch(Exception ex) {
    // TODO neither password worked - what do we do now? [insert code here...]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文