Impostazioni di protezione avanzata da Excel XP in poi
Introduzione
Queste impostazioni di protezione limitano o consentono agli utenti di:
- Eliminare righe o colonne.
- Modificare contenuti, oggetti o scenari.
- Formattare celle, righe o colonne.
- Inserire righe, colonne o collegamenti ipertestuali.
- Selezionare celle bloccate o sbloccate.
- Usare tabelle pivot e molto altro.
Aspose.Cells supporta tutte le impostazioni di protezione avanzate offerte da Excel XP o versioni successive.
Impostazioni di protezione avanzate utilizzando Excel XP e versioni successive
Per visualizzare le impostazioni di protezione disponibili in Excel XP:
- Dal menu Strumenti, seleziona Protezione seguito da Proteggi foglio. Verrà visualizzata una finestra di dialogo.
Per visualizzare le impostazioni di protezione disponibili in Excel 2016
- Dal menu File, seleziona Proteggi workbook seguito da Proteggi foglio attivo.
- Seleziona Proteggi foglio nel menu Revisione.
Seguendo i passaggi sopra menzionati verrà visualizzata una finestra di dialogo in cui è possibile consentire o limitare le funzionalità dei fogli di lavoro o applicare una password al foglio di lavoro.
Impostazioni di protezione avanzate utilizzando Aspose.Cells
Aspose.Cells supporta tutte le impostazioni avanzate di protezione.
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente l’accesso a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet.
La classe Worksheet fornisce la proprietà Protection che viene utilizzata per applicare queste impostazioni avanzate di protezione. La proprietà Protection è infatti un oggetto della classe Protection che incapsula diverse proprietà booleane per disabilitare o abilitare restrizioni.
Di seguito è riportato un piccolo esempio di applicazione. Apre un file Excel e utilizza la maggior parte delle impostazioni avanzate di protezione supportate da Excel XP e versioni successive.
// 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(); |
Problema di blocco delle celle
Se si desidera impedire agli utenti di modificare le celle, le celle devono essere bloccate prima di applicare qualsiasi impostazione di protezione. In caso contrario, le celle possono essere modificate anche se il foglio di lavoro è protetto. In Microsoft Excel XP, le celle possono essere bloccate tramite la seguente finestra di dialogo:
Finestra di dialogo per bloccare le celle in Excel XP |
---|
![]() |
È possibile bloccare le celle anche utilizzando l’API Aspose.Cells. Ogni cella può ottenere una formattazione che contiene una proprietà booleana, IsLocked. Impostare la proprietà IsLocked su true o false per bloccare o sbloccare la cella.
// 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"); | |