簡體   English   中英

使用允許排序和允許自動過濾來保護 Excel C#

[英]Protect Excel With Allow Sort and Allow Auto Filter C#

我正在使用EPPLUS.dll做我的 excel 工作我試圖使用AllowSortAllowAutoFilter屬性保護工作表,但它不起作用。

worksheet.Protection.AllowSort = true;
worksheet.Protection.AllowAutoFilter = true;
worksheet.Protection.SetPassword("password");
worksheet.Protection.IsProtected = true;

我也試過下面的代碼,但我沒有得到我想要的結果

worksheet.Column(1).Style.Locked = true;

默認情況下,Epplus Lock Cells 和 Locking Cells 不會解決我的問題; 我唯一需要的是一個受保護或只讀的單元格,同時允許排序和過濾

任何幫助將不勝感激。

您是否嘗試過移動worksheet.Protection.IsProtected = true; 成為代碼塊中的第一條語句。 也許這覆蓋了您之前的陳述。

https://github.com/pruiz/EPPlus/blob/master/EPPlus/ExcelSheetProtection.cs

以下內容過去對我有用,因為它將保護工作表,即使其只讀

        worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
        worksheet.Protection.IsProtected = true;
        worksheet.View.FreezePanes(2, 1); // freeze header row
        worksheet.Protection.AllowSort = true;
        worksheet.Cells[worksheet.Dimension.Address].AutoFilter = true;
        worksheet.Protection.AllowAutoFilter = true;

我認為密碼保護它,它必須針對包而不是工作表。

因此,如果保存包,請使用 .Save 重載。

package.Save("password");

或者,如果您要保存為 ByteArray,則

package.GetAsByteArray("password");

我發現的唯一方法是在 Excel 文檔中使用 Visual Basic 代碼,該代碼鎖定了右鍵單擊和復制粘貼選項。 不幸的是,在 excel 打開文檔之前,它會詢問有關危險腳本的信息並提供禁用正在運行的腳本。 最后,在我的情況下,我是不可能的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM