检测工作表是否受密码保护
Contents
[
Hide
]
可以分别保护工作簿和工作表。例如,电子表格可能包含一个或多个受密码保护的工作表,但电子表格本身可能受到保护或未受到保护。Aspose.Cells API提供了一种检测给定工作表是否受密码保护的方法。本文演示了使用Aspose.Cells for Java API实现相同功能。
检测工作表是否受密码保护
Aspose.Cells for Java 8.7.0已公开了Protection.isProtectedWithPassword属性,用于检测工作表是否受密码保护。布尔类型Protection.isProtectedWithPassword字段返回true,如果Worksheet受到密码保护,返回false,如果没有。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DetectWorksheetisPasswordProtected.class); | |
// Create an instance of Workbook and load a spreadsheet | |
Workbook book = new Workbook(dataDir + "sample.xlsx"); | |
// Access the protected Worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Check if Worksheet is password protected | |
if (sheet.getProtection().isProtectedWithPassword()) { | |
System.out.println("Worksheet is password protected"); | |
} |