Excel XP以降の高度な保護設定
紹介
これらの保護設定により、ユーザーは次の操作を制限または許可できます:
- 行または列の削除。
- 内容、オブジェクト、またはシナリオの編集。
- セル、行、または列の書式設定。
- 行、列、またはハイパーリンクの挿入。
- ロックされたセルまたはロックされていないセルの選択。
- ピボットテーブルなどの使用。
Aspose.CellsはExcel XP以降のバージョンで提供されるすべての高度な保護設定をサポートしています。
Excel XPおよびそれ以降のバージョンを使用した高度な保護設定
Excel XPで利用可能な保護設定を表示するには:
- ツールメニューから、保護の後にシートを保護を選択します。ダイアログが表示されます。
Excel 2016で利用可能な保護設定を表示するには
- ファイルメニューから、ワークブックを保護, その後現在のシートを保護を選択します。
- レビューメニューでシートを保護を選択します。
上記の手順に従うと、ワークシートの機能を許可または制限したり、ワークシートにパスワードを適用したりするダイアログが表示されます。
Aspose.Cellsを使用した高度な保護設定
Aspose.Cellsはすべての高度な保護設定をサポートしています。
Aspose.Cells は Microsoft Excel ファイルを表す Workbook クラスを提供します。Workbook クラスには、Excel ファイル内の各ワークシートにアクセスするための Worksheets コレクションが含まれています。ワークシートは Worksheet クラスで表されます。
Worksheetクラスは、これらの高度な保護設定を適用するために使用されるProtectionプロパティを提供します。Protectionプロパティは実際にはProtectionクラスのオブジェクトであり、無効化または有効化するための複数のブール値プロパティをカプセル化しています。
以下は小さなサンプルアプリケーションです。それはExcelファイルを開いて、Excel XPおよびそれ以降のバージョンでサポートされる高度な保護設定のほとんどを使用します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook excel = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = excel.Worksheets[0]; | |
// Restricting users to delete columns of the worksheet | |
worksheet.Protection.AllowDeletingColumn = false; | |
// Restricting users to delete row of the worksheet | |
worksheet.Protection.AllowDeletingRow = false; | |
// Restricting users to edit contents of the worksheet | |
worksheet.Protection.AllowEditingContent = false; | |
// Restricting users to edit objects of the worksheet | |
worksheet.Protection.AllowEditingObject = false; | |
// Restricting users to edit scenarios of the worksheet | |
worksheet.Protection.AllowEditingScenario = false; | |
// Restricting users to filter | |
worksheet.Protection.AllowFiltering = false; | |
// Allowing users to format cells of the worksheet | |
worksheet.Protection.AllowFormattingCell = true; | |
// Allowing users to format rows of the worksheet | |
worksheet.Protection.AllowFormattingRow = true; | |
// Allowing users to insert columns in the worksheet | |
worksheet.Protection.AllowFormattingColumn = true; | |
// Allowing users to insert hyperlinks in the worksheet | |
worksheet.Protection.AllowInsertingHyperlink = true; | |
// Allowing users to insert rows in the worksheet | |
worksheet.Protection.AllowInsertingRow = true; | |
// Allowing users to select locked cells of the worksheet | |
worksheet.Protection.AllowSelectingLockedCell = true; | |
// Allowing users to select unlocked cells of the worksheet | |
worksheet.Protection.AllowSelectingUnlockedCell = true; | |
// Allowing users to sort | |
worksheet.Protection.AllowSorting = true; | |
// Allowing users to use pivot tables in the worksheet | |
worksheet.Protection.AllowUsingPivotTable = true; | |
// Saving the modified Excel file | |
excel.Save(dataDir + "output.xls", SaveFormat.Excel97To2003); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
セルロックの問題
セルの編集を制限したい場合は、保護設定を適用する前にセルをロックする必要があります。そうでない場合、ワークシートが保護されていてもセルが編集できます。Microsoft Excel XPでは、次のダイアログでセルをロックできます:
Excel XPでセルをロックするダイアログ |
---|
![]() |
Aspose.Cells APIを使用してセルをロックすることも可能です。各セルには、セルをロックまたは解除するためのブール値プロパティIsLockedを含むStyle書式を取得できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Cells["A1"].GetStyle().IsLocked = true; | |
// Finally, Protect the sheet now. | |
worksheet.Protect(ProtectionType.All); | |
workbook.Save(dataDir + "output.xlsx"); | |