使用 Open XML SDK 保护 Excel 文件密码 [英] Excel File Password Protection with Open XML SDK

查看:23
本文介绍了使用 Open XML SDK 保护 Excel 文件密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Open XML SDK 创建 excel 文件.

I am using Open XML SDK for creating excel files.

我想用密码保护他们.

您知道如何使用 Open XML SDK 使用密码保护 excel 文件吗?

Do you know anyway to protect excel file with a password by using Open XML SDK?

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

I know "com" object way to protect them however, it is not suitable for my application. I need to protect file by using Open XML SDK or another way.

推荐答案

可以通过打开 xml 创建一个用于保护工作簿或工作表的 excel 密码.

Creating an excel password for protecting workbook or worksheet is possible by open xml.

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

Following code samples are suggestions of Vincent (http://spreadsheetlight.com/about/) (https://stackoverflow.com/users/12984/vincent-tan) (again I thank him a lot :)

        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();
           }
        }

如果你有图表之类的,那么

If you have a chart or something then

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

Following code samples are suggestions of Vincent (http://spreadsheetlight.com/about/) (https://stackoverflow.com/users/12984/vincent-tan) (again I thank him a lot :)

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();
    }
}

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

These methods makes a protection that any user cant change the data accidentally. However, if you do not want any user that don't know password to see the data then you can use following library:

http://dotnetzip.codeplex.com/

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

You have a password protected zipped file that contains your excel.xlsx file by using the dotnetzip library.

一个例子:

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);
    }
}

这篇关于使用 Open XML SDK 保护 Excel 文件密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆