Kryptering av Excel filer

Använda Microsoft Excel

För att ställa in filkrypteringsinställningar i Microsoft Excel (här Microsoft Excel 2003):

  1. Från menyn Verktyg, välj Alternativ. En dialogruta kommer att visas.
  2. Välj fliken Säkerhet.
  3. Ange ett lösenord och klicka på Avancerat
  4. Välj krypteringstyp och bekräfta lösenordet.

Kryptering med Aspose.Cells

Exemplet nedan visar hur man krypterar och lösenordsskyddar en Excel-fil med hjälp av Aspose.Cells API.

// 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);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Specify XOR encryption type.
workbook.SetEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.SetEncryptionOptions(EncryptionType.StrongCryptographicProvider, 128);
// Password protect the file.
workbook.Settings.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "encryptedBook1.out.xls");

Ange lösenord för att ändra alternativ

Följande exempel visar hur man ställer in alternativet Lösenord för att ändra i Microsoft Excel för en befintlig fil med hjälp av Aspose.Cells API.

// 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);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Set the password for modification.
workbook.Settings.WriteProtection.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "SpecifyPasswordToModifyOption.out.xls");

Verifiera lösenordet för den krypterade filen

För att verifiera lösenordet för den krypterade filen tillhandahåller Aspose.Cells for .NET VerifyPassword metoden. Dessa metoder accepterar två parametrar, filströmmen och lösenordet som ska verifieras. Följande kodavsnitt demonstrerar användningen av metod VerifyPassword för att verifiera om det angivna lösenordet är giltigt eller inte.

// 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);
// Create a Stream object
FileStream fstream = new FileStream(dataDir + "EncryptedBook1.xlsx", FileMode.Open);
bool isPasswordValid = FileFormatUtil.VerifyPassword(fstream, "1234");
Console.WriteLine("Password is Valid: " + isPasswordValid);

Kryptering/Dekryptering av ODS-fil med Aspose.Cells

Aspose.Cells tillåter att kryptera och dekryptera ODS-fil. Dekrypterad ODS-fil kan öppnas både i Excel och OpenOffice, men krypterad ODS-fil kan endast öppnas av OpenOffice efter att lösenordet har angetts. Excel kan inte öppna den krypterade ODS-filen och kan visa varningsmeddelande. Krypteringsalternativen gäller inte för ODS-fil som för andra filtyper. För att kryptera en ODS-fil laddar du in filen och anger det WorkbookSettings.Password värde till det faktiska lösenordet innan du sparar den. Den krypterade ODS-filen kan öppnas i OpenOffice endast.

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an ODS file
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods");
// Password protect the file
workbook.Settings.Password = "1234";
// Save the ODS file
workbook.Save(outputDir + "outputEncryptedODSFile.ods");

För att dekryptera en ODS-fil, ladda in filen genom att ange ett lösenord i LoadOptions.Password. När filen har laddats, ställ in strängen för WorkbookSettings.Password till null.

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an encrypted ODS file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Ods);
// Set original password
loadOptions.Password = "1234";
// Load the encrypted ODS file with the appropriate load options
Workbook workbook = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.Settings.Password = null;
// Save the decrypted ODS file
workbook.Save(outputDir + "outputDecryptedODSFile.ods");