使用 Epplus 的受保护工作表 - 需要允许过滤和排序。不允许排序
Protected worksheet with Epplus - need to allow filtering and sorting. Sorting not permitted
以下方法的目的是在使用 Epplus 向工作表写入数据后设置样式和保护工作表。样式正确,适当的单元格(在特定行中)受到保护。它旨在允许排序和过滤,尽管“header”行单元格受到保护。
在生成的电子表格中,Excel 允许执行过滤。但是,排序会遇到错误消息。如何解决?
public void FormatSpreadsheet(ExcelWorksheet worksheet)
{
ExcelRange range;
// styling header cells...
range.AutoFilter = true;
// protect headers and allow sorting and filtering, amongst other things
worksheet.Protection.IsProtected = true;
worksheet.Protection.AllowAutoFilter = true;
worksheet.Protection.AllowDeleteColumns = false;
worksheet.Protection.AllowDeleteRows = true;
worksheet.Protection.AllowEditObject = true;
worksheet.Protection.AllowEditScenarios = true;
worksheet.Protection.AllowFormatCells = true;
worksheet.Protection.AllowFormatColumns = true;
worksheet.Protection.AllowFormatRows = true;
worksheet.Protection.AllowInsertColumns = false;
worksheet.Protection.AllowInsertHyperlinks = false;
worksheet.Protection.AllowInsertRows = true;
worksheet.Protection.AllowPivotTables = false;
worksheet.Protection.AllowSelectLockedCells = true;
worksheet.Protection.AllowSelectUnlockedCells = true;
worksheet.Protection.AllowSort = true;
// set a random password so it's impossible for anybody to edit the protected cells...
// autofit columns
range = worksheet.Cells[1, 1, worksheet.Dimension.End.Row, lastHeaderCol];
range.AutoFitColumns();
}
您实际上需要激活 自动过滤器,而不仅仅是允许 它。要使用工作表的第一行作为 header 进行过滤,上面的 range
object 将起作用,您只需要再设置一个 属性:
range.AutoFilter = true;
以下方法的目的是在使用 Epplus 向工作表写入数据后设置样式和保护工作表。样式正确,适当的单元格(在特定行中)受到保护。它旨在允许排序和过滤,尽管“header”行单元格受到保护。
在生成的电子表格中,Excel 允许执行过滤。但是,排序会遇到错误消息。如何解决?
public void FormatSpreadsheet(ExcelWorksheet worksheet)
{
ExcelRange range;
// styling header cells...
range.AutoFilter = true;
// protect headers and allow sorting and filtering, amongst other things
worksheet.Protection.IsProtected = true;
worksheet.Protection.AllowAutoFilter = true;
worksheet.Protection.AllowDeleteColumns = false;
worksheet.Protection.AllowDeleteRows = true;
worksheet.Protection.AllowEditObject = true;
worksheet.Protection.AllowEditScenarios = true;
worksheet.Protection.AllowFormatCells = true;
worksheet.Protection.AllowFormatColumns = true;
worksheet.Protection.AllowFormatRows = true;
worksheet.Protection.AllowInsertColumns = false;
worksheet.Protection.AllowInsertHyperlinks = false;
worksheet.Protection.AllowInsertRows = true;
worksheet.Protection.AllowPivotTables = false;
worksheet.Protection.AllowSelectLockedCells = true;
worksheet.Protection.AllowSelectUnlockedCells = true;
worksheet.Protection.AllowSort = true;
// set a random password so it's impossible for anybody to edit the protected cells...
// autofit columns
range = worksheet.Cells[1, 1, worksheet.Dimension.End.Row, lastHeaderCol];
range.AutoFitColumns();
}
您实际上需要激活 自动过滤器,而不仅仅是允许 它。要使用工作表的第一行作为 header 进行过滤,上面的 range
object 将起作用,您只需要再设置一个 属性:
range.AutoFilter = true;