实现一个场景,exce了在被保护的情况下是不能进行其他任何操作表单的操作的, 现在需要放开一些权限,使数据在被只读的情况下依然可以进行filter功能。
直接上答案:
sheet.setAutoFilter(new CellRangeAddress(rowStart , rowEnd, colStart,colEnd));
sheet.lockAutoFilter(false);
sheet.lockFormatColumns(false);
sheet.lockFormatRows(false);
sheet.lockFormatCells(false);
//Also need to provide password ,whereas it has no reponse if you set it as true
sheet.lockSort(false);
sheet.protectSheet("password");
注意单元格默认为locked, 需要将要求可编辑的单元格设置为locked为false的状态
参考资料:
https://stackoverflow.com/questions/13703441/setting-filter-on-headers-of-an-excel-sheet-via-poi
https://stackoverflow.com/questions/14701322/apache-poi-how-to-protect-sheet-with-options
两种开放filter权限的方式都可以,不过使用CTSheetProtection要将值设置为false来表示开放权限
POI操作Excel 也是要求excel中有的功能才能实现,所以这里对excel操作的熟练度,也是保证编程速度的一种方式。
如何在excel中设置保护表单的情况下加入其它操作功能:
https://www.extendoffice.com/documents/excel/4673-excel-sort-filter-protected-sheet.html
https://blog.softartisans.com/2013/10/01/kb-sorting-locked-cells-in-protected-worksheets/
注意: Allow users to edit ranges 此选项,在windows版本中存在,在Mac中并不存在,Mac版本中,直接在projectsheet的选项中勾选相应的功能即可。
另一个思路是加入VB代码:
在表单名字下右键选择view code,贴上代码保存:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Or Target.Row = 1 Or Target.Row = 2 Or Target.Row = 3 Then
Beep
MsgBox Cells(Target.Row, Target.Column).Address & " cannot be selected or edited as it is a read-only cell"
Cells(5, 2).Select
End If
If Target.Row = 4 Then
If Target.Column <> 1 Then
Beep
MsgBox ("Please don't change the cell value when you manipulate")
End If
End If
End Sub
但是这种情况要保存为enable marco的文件形式,而且在打开会弹出提示要不要允许Marco执行,允许你加入的VB代码才能生效,点击否则不会生效,doesn't make sense or have any use.
参考资料:
https://www.extendoffice.com/documents/excel/4078-excel-make-cell-read-only.html
网友评论