简体   繁体   中英

Excel File Password Protection with Open XML SDK

I am using Open XML SDK for creating excel files.

I want to protect them with a password.

Do you know anyway to protect excel file with a password by using 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.

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

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

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/

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

An example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM