none
How to UnProtect Sheet using OpenXML code? RRS feed

  • Question

  • Hello,

    I have created one Sheet, which i could protect using OpenXml code.

    But now there is requirement to read this excel file.

    I am getting all the values as NULL because it is protected.

    ( I haven't placed any password yet in the code to protect the sheet, there is only one sheet in the excel file.)

    I have got below code from my Search to unprotect the worksheet. 

          workSheet.RemoveAllChildren<SheetProtection>();

    But, this is not working. I am still getting the null values while reading this protected sheet.

    Below is my code example.

                    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(FilePath, false))
                    {
                        WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
                        IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
                        //if ((sheets.Count() != 2) && (sheets.First().Name.Value != "StudentNomination") && (sheets.Last().Name.Value != "Sheet2"))
                        //{
                        //    throw new Exception("Please Upload the correct Nomination file, for example you can download the Nomination Template file first.!!");
                        //}
                        string relationshipId = sheets.First().Id.Value;
                        WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
                        Worksheet workSheet = worksheetPart.Worksheet;
                        workSheet.RemoveAllChildren<SheetProtection>();
                        SheetData sheetData = workSheet.GetFirstChild<SheetData>();
                        IEnumerable<Row> rows = sheetData.Descendants<Row>();

    Can anyone Please help me on this?

    Monday, May 20, 2013 4:31 AM

Answers