[英]How to password protect an Excel document with Open XML
目前,我正在通過傳遞MemoryStream
參數使用 Open XML 的SpreadsheetDocument
類創建一個新的 Excel 文檔。 我現在需要在這個SpreadsheetDocument
對象上設置密碼,但我嘗試的似乎不起作用。 無需輸入密碼即可打開 Excel 文檔。 以下是我到目前為止所嘗試的( mem
是MemoryStream
參數):
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mem, true))
{
foreach (var sheet in spreadsheet.WorkbookPart.WorksheetParts)
{
sheet.Worksheet.Append(new SheetProtection() { Password = "test" });
}
}
我也嘗試了以下但沒有成功:
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mem, true))
{
spreadsheet.WorkbookPart.Workbook.WorkbookProtection = new WorkbookProtection
{
LockStructure = true,
LockWindows = true,
WorkbookPassword = "test"
}
}
請問我錯過了什么?
Openxml 表保護密碼的輸入數據類型為“HexBinaryValue”。 所以輸入密碼要從字符串轉換為十六進制二進制。
foreach (var worksheetPart in spreadsheet.WorkbookPart.WorksheetParts)
{
//Call the method to convert the Password string "MyPasswordfor sheet" to hexbinary type
string hexConvertedPassword = HexPasswordConversion("MyPasswordfor sheet");
//passing the Converted password to sheet protection
SheetProtection sheetProt = new SheetProtection() { Sheet = true, Objects = true, Scenarios = true, Password = hexConvertedPassword };
worksheetPart.Worksheet.InsertAfter(sheetProt,worksheetPart.Worksheet.Descendants<SheetData>().LastOrDefault());
worksheetPart.Worksheet.Save();
}
/* This method will convert the string password to hexabinary value */
protected string HexPasswordConversion(string password)
{
byte[] passwordCharacters = System.Text.Encoding.ASCII.GetBytes(password);
int hash = 0;
if (passwordCharacters.Length > 0)
{
int charIndex = passwordCharacters.Length;
while (charIndex-- > 0)
{
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
hash ^= passwordCharacters[charIndex];
}
// Main difference from spec, also hash with charcount
hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
hash ^= passwordCharacters.Length;
hash ^= (0x8000 | ('N' << 8) | 'K');
}
return Convert.ToString(hash, 16).ToUpperInvariant();
}
好吧,這不完全是我想要做的,但我最終放棄了 Open XML SDK 並使用 Office.Interop 程序集來保護文檔。 首先使用 Open XML 的原因是因為似乎無法使用流打開 Interop 工作簿,它需要一個實際文件。
你可以試試這個:
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(mem, true))
{
foreach (var worksheetPart in spreadsheet.WorkbookPart.WorksheetParts)
{
SheetProtection sheetProt = new SheetProtection() { Sheet = true, Objects = true, Scenarios = true, Password = "test" };
worksheetPart.Worksheet.InsertAfter(sheetProt, worksheetPart.Worksheet.Descendants<SheetData>().LastOrDefault());
}
}
禁止在超級圖書館(Libre)中使用坦帕拉姆語(OfficeOpenXml)禁止在任何情況下都可以使用永久性文件保護合同。 OpenXML的獨創性和檔案管理的差異性,反之亦然。
OfficeOpenXml的pequeñoproyecto產品
using OfficeOpenXml;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Create a new ExcelPackage
using (ExcelPackage excelPackage = new ExcelPackage())
{
//Set some properties of the Excel document
excelPackage.Workbook.Properties.Author = "VDWWD";
excelPackage.Workbook.Properties.Title = "Title of Document";
excelPackage.Workbook.Properties.Subject = "EPPlus demo export data";
//Create the WorkSheet
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
//Add some text to cell A1
worksheet.Cells["A1"].Value = "My first EPPlus spreadsheet!";
//You could also use [line, column] notation:
worksheet.Cells[1, 2].Value = "This is cell B1!";
//Save your file
FileInfo fi = new FileInfo(@"D:\PruebasXMLS\File.xlsx");
excelPackage.SaveAs(fi, "123456789");
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.