有谁知道这个的语法?我一直在到处寻找,我能找到的只有 C++ 代码。我正在尝试使用 System.IO.Packaging 命名空间以编程方式对 excel 文件进行密码保护。
有任何想法吗?
补充笔记:
我没有使用 Excel 互操作,而是使用 System.IO.Packaging 命名空间来加密和密码保护 Excel 文件。
有谁知道这个的语法?我一直在到处寻找,我能找到的只有 C++ 代码。我正在尝试使用 System.IO.Packaging 命名空间以编程方式对 excel 文件进行密码保护。
有任何想法吗?
补充笔记:
我没有使用 Excel 互操作,而是使用 System.IO.Packaging 命名空间来加密和密码保护 Excel 文件。
如果你想要一个 Excel 密码,你只需要这样:
using Microsoft.Office.Interop.Excel
//create your spreadsheet here...
WorkbookObject.Password = password;
WorkbookObject.SaveAs("spreadsheet.xls")
这需要安装 Excel。
当然,这无关紧要System.IO.Packaging
,因此您可能需要重申您的问题...
使用 是不可能的System.IO.Packaging
。您将不得不Microsoft.Office.Interop.Excel
使用该Worksheet.SaveAs
方法。这需要在您的目标系统上安装 Excel。
您将不得不在工作表上使用 SaveAs 方法。它有一个设置密码的参数。这是VB中的一个示例,可以转换为C#
using System.IO;
using Excel=Microsoft.Office.Interop.Excel;
class ExcelUtil
{
public string Filename;
private Excel.Application oexcel;
private Excel.Workbook obook;
private Excel.Worksheet osheet;
public void createPwdExcel()
{
try
{
// File name and path, here i used abc file to be
// stored in Bin directory in the sloution directory
//Filename = (AppDomain.CurrentDomain.BaseDirectory + "abc.xls");
if (File.Exists(Filename))
{
File.Delete(Filename);
}
if (!File.Exists(Filename))
{
// create new excel application
Excel.Application oexcel = new Excel.Application();
oexcel.Application.DisplayAlerts = false;
obook = oexcel.Application.Workbooks.Add(Type.Missing);
oexcel.Visible = true;
Console.WriteLine("Generating Auto Report");
osheet = (Excel.Worksheet)obook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
osheet.Name = "Test Sheet";
osheet.get_Range("A1:G1").Merge();
osheet.get_Range("A1").Value = @"Implementing Password Security on Excel Workbook Using Studio.Net";
osheet.get_Range("A1").Interior.ColorIndex = 5;
osheet.get_Range("A1").Font.Bold = true;
string password = "abc";
obook.WritePassword = password;
obook.SaveAs("Chandra.xlsx");
// otherwise use the folowing one
// TODO: Labeled Arguments not supported. Argument: 2 := 'password'
// end application object and session
osheet = null;
obook.Close();
obook = null;
oexcel.Quit();
oexcel = null;
}
}
catch (Exception ex)
{
}
}
}
从 NuGet 包下载 Microsoft.Office.Interop.Excel。使用下面的 c# 代码密码保护 Excel 文件
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(srcFile);
//protect the whole workbook
xlWorkbook.Password = "passwordString";
xlWorkbook.SaveAs(dstFile);
xlWorkbook.Close();
xlApp.Quit();