Excel文件密码保护与开放XML SDK [英] Excel File Password Protection with Open XML SDK

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

问题描述

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

I am using Open XML SDK for creating excel files.

我想用密码保护他们。

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

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.

推荐答案

创建用于保护手机或工作表的excel密码是可能的通过打开xml。

Creating an excel password for protecting woorkbook 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/

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

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

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

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