C# Workbook Encryption (Password Protected) SpreadsheetGear API Sample

Excel-Compatible Samples for .NET

Description

SpreadsheetGear supports reading and writing Excel-compatible password-protected and encrypted workbooks with "Agile Encryption" used by Excel 2013 and later, including default SHA-512 encryption for workbooks. To save a workbook file with SpreadsheetGear that uses this password protection and encryption, simply call the overload of the SpreadsheetGear.IWorkbook.SaveAs(...) method that accepts a password as the 3rd parameter, as demonstrated in this sample:

Sample C# Source Code

// Create a new workbook and some local variables for convenience.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
SpreadsheetGear.IRange cells = worksheet.Cells;

// Rename the worksheet.
worksheet.Name = "My Worksheet";

// Add some cell data.
cells["A1"].Value = "Hello World!";

// Save to Excel to Open XML (*.xlsx) file.
workbook.SaveAs(@"c:\path\to\workbook.xlsx", 
    SpreadsheetGear.FileFormat.OpenXMLWorkbook, "MyPassword1234");