加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Open XML SDK保护Excel文件密码

发布时间:2020-12-16 07:56:14 所属栏目:百科 来源:网络整理
导读:我正在使用Open XML SDK来创建excel文件. 我想用密码保护他们. 您是否知道使用Open XML SDK保护excel文件的密码? 我知道“com”对象的方式来保护它们,但它不适合我的应用程序.我需要使用Open XML SDK或其他方式保护文件. 打开xml可以创建用于保护工作簿或工
我正在使用Open XML SDK来创建excel文件.

我想用密码保护他们.

您是否知道使用Open XML SDK保护excel文件的密码?

我知道“com”对象的方式来保护它们,但它不适合我的应用程序.我需要使用Open XML SDK或其他方式保护文件.

打开xml可以创建用于保护工作簿或工作表的Excel密码.

以下代码示例是Vincent(http://spreadsheetlight.com/about/)(https://stackoverflow.com/users/12984/vincent-tan)的建议(我再次感谢他:)

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docname,true))
        {
            foreach (var worksheet in spreadSheet.WorkbookPart.WorksheetParts)
           {
                worksheet.Worksheet.Append(new SheetProtection(){ Password = “CC”});
               // add this in case it still doesn’t work. This makes sure the data is saved.
               //worksheet.Worksheet.Save();
           }
        }

如果你有图表或其他东西

以下代码示例是Vincent(http://spreadsheetlight.com/about/)(https://stackoverflow.com/users/12984/vincent-tan)的建议(我再次感谢他:)

bool bFound;
OpenXmlElement oxe;
SheetProtection prot;
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open("OtoPark.xlsx",true))
{
    foreach (var worksheet in spreadSheet.WorkbookPart.WorksheetParts)
    {
        prot = new SheetProtection();
        prot.Password = "CC";
        // these are the "default" Excel settings when you do a normal protect
        prot.Sheet = true;
        prot.Objects = true;
        prot.Scenarios = true;

        // Open up Excel and do a password protect yourself and use the
        // Productivity Tool to see the property values of the resulting Excel file.
        // Consider not using the Password property and use:
        //prot.AlgorithmName = "SHA-512";
        //prot.HashValue = "somehashvaluebythealgorithm";
        //prot.SaltValue = "somesalt";
        //prot.SpinCount = 100000;

        bFound = false;
        oxe = worksheet.Worksheet.FirstChild;
        foreach (var child in worksheet.Worksheet.ChildElements)
        {
            // start with SheetData because it's a required child element
            if (child is SheetData || child is SheetCalculationProperties)
            {
                oxe = child;
                bFound = true;
            }
        }

        if (bFound)
        {
            worksheet.Worksheet.InsertAfter(prot,oxe);
        }
        else
        {
            worksheet.Worksheet.PrependChild(prot);
        }

        worksheet.Worksheet.Save();
    }
}

这些方法可以保护任何用户无法意外更改数据.但是,如果您不希望任何不知道密码的用户查看数据,那么您可以使用以下库:

http://dotnetzip.codeplex.com/

您有一个受密码保护的压缩文件,其中包含您使用dotnetzip库的excel.xlsx文件.

一个例子:

public void RNCreateZipFile(string ExcelDocName,string PassWord,string ZipDocName)
{
    // create a zip
    using (var zip = new ZipFile())
    {
        zip.Password = PassWord;
        zip.AddFile(ExcelDocName,"");
        zip.Save(ZipDocName);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读