Worksheet protection. IsLocked=false not working

Sep 10, 2014 at 2:52 PM
Edited Sep 10, 2014 at 3:04 PM
Hi all,

I'm new to npoi, and i'm trying to protect a worksheet while leaving certain cells open for editing. I've tried everything i can think of.

The sample below is a merger of two of the npoi tutorials

I then tried setting IsLocked = false, but the whole sheet is still protected.

Your help is greatly appreciated

I'm using NPOI v 2.1.1.0
        IWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet1=(XSSFSheet)workbook.CreateSheet("Sheet A1");

        sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample");
        int x = 1;
        for (int i = 1; i <= 15; i++)
        {
            IRow row = sheet1.CreateRow(i);
            for (int j = 0; j < 15; j++)
            {
                var cell = row.CreateCell(j);
                //cell.CellStyle.IsLocked = false;
                var unlockedstyle = workbook.CreateCellStyle();
                unlockedstyle.IsLocked = false;

                //((Range)xlWorkSheet.Cells[y + 1, x + 1]).Locked = false;
                cell.CellStyle = unlockedstyle;

                cell.SetCellValue(x++);

            }
        }

        sheet1.LockFormatRows();
        sheet1.LockFormatCells();
        sheet1.LockFormatColumns();
        sheet1.LockDeleteColumns();
        sheet1.LockDeleteRows();
        sheet1.LockInsertHyperlinks();
        sheet1.LockInsertColumns();
        sheet1.LockInsertRows();
        sheet1.ProtectSheet("password");


        FileStream sw = File.Create("test.xlsx");
        workbook.Write(sw);
        sw.Close();
I'm using NPOI v 2.1.1.0
Sep 10, 2014 at 3:12 PM
Looks like this is a bug with v2.1.1.0
I downgraded to v2.0.6.0 (2.6) and it worked perfectly
Dec 23, 2014 at 9:41 AM
Same problem here with XSSF. Downgrade is no option, will dig into the code.
Dec 23, 2014 at 9:53 AM
Fixed with pull request #34.

https://github.com/tonyqus/npoi/pull/34