Excel File Password Protection with Open XML SDK

后端 未结 1 515
孤街浪徒
孤街浪徒 2020-12-11 12:54

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

相关标签:
1条回答
  • 2020-12-11 13:57

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题