Search code examples
c#excelnpoi

NPOI protect sheet XSSF excel workbook


I have to protect only one row (example the rowNum 0) in a xssf Excel generated in this way (using library NPOI version 2.2.1.0)

ISheet sheet_for_dropdown_list = Workbook.CreateSheet("DropDownList");

Actually the protect command is used only to protect entirely sheet by means of a password

sheet_for_dropdown_list.ProtectSheet("admin");

Then I tryed to unlock the row1 setting the property isLocked false in a style

ICellStyle extCellStyle = Workbook.CreateCellStyle();

unprotectCellStyle.IsLocked = false

IRow1 row1= sheet_for_dropdown_list.GetRow(1);

and unprotect all cell

foreach (ICell cell in row1)
        cell.CellStyle = unprotectCellStyle;

This operation could be done for all the row > 0

This solution partially works, because is possible to modify the cell contents but isn't possible to modify the height of the column. So in the free cell isn't possible to insert contents with height greater than the actual height column.


Solution

  • I think you are looking to set the height of header row. You can either assign the height or wrap the text or both.

    To assign the height see this link. To Wrap a text use below syntax

    XSSFCellStyle headerStyle = (XSSFCellStyle)workbook.CreateCellStyle();
    headerStyle.WrapText = true;