C# NPOI 锁定单元格设置只读
锁定单元格原理 给所有单元格赋值上锁样式,如有不设置只读的,赋值为未上锁样式,主要代码
ISheet sheet = workBook.CreateSheet(sheetName); workBook.GetSheetAt(0).ProtectSheet("password"); //解除锁定样式 ICellStyle cellstyleUnLock = workBook.CreateCellStyle(); cellstyleUnLock.IsLocked = false;//解锁 //锁定样式 ICellStyle style1 = workBook.CreateCellStyle(); style1.IsLocked = true;//上锁 foreach (DataColumn column in sourceTable.Columns) { //支付金额允许修改 if (column.ColumnName != "支付金额") dataRow.GetCell(column.Ordinal).CellStyle = style; else dataRow.GetCell(column.Ordinal).CellStyle = cellstyleUnLock; }