Sort and UseAutoFilter not working while Protect Excel sheet in win c#

Gani_tpt 2,026 Reputation points

I am trying to apply sort and autofilter while protecting excel file in win c#.

but, i am getting error. what is it..?


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,461 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points

    Hi GaniTPT,
    What did you use to access worksheets?
    I made a test by using EPPlus and it worked fine.
    Here is a test code you can refer to:

    ExcelPackage.LicenseContext = LicenseContext.Commercial;  
        using (var p = new ExcelPackage(new FileInfo(@"C:\Users\Desktop\Test.xlsx")))  
            var ws = p.Workbook.Worksheets["Sheet1"];  
            // Filtering, sorting, protection  
            ws.Cells[7, 1, 10, 5].AutoFilter = true;  
            ws.View.FreezePanes(7, 1);  
            ws.ProtectedRanges.Add("FilteredCells", new ExcelAddress(7, 1, 10, 5));  
            // Worksheet protection  
            ws.Protection.AllowSort = true;  
            ws.Protection.IsProtected = true;  
            ws.Protection.AllowAutoFilter = true;  
            p.SaveAs(new FileInfo(@"C:\Users\Desktop\Test1.xlsx"));  

    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful