【发布时间】:2021-04-24 07:21:51
【问题描述】:
以下方法的目的是在使用 Epplus 将数据写入工作表后对其进行样式设置和保护。样式正确,并且适当的单元格(在特定行中)受到保护。它旨在允许排序和过滤,尽管“标题”行单元格受到保护。
在生成的电子表格中,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();
}
【问题讨论】:
-
错误信息是什么?
-
@AhmadBagadood “您尝试更改的单元格或图表位于受保护的工作表上”。
标签: c# .net excel spreadsheet epplus