在 C# 中密码保护 Excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Password Protecting an Excel file in C#
提问by yeahumok
Does anyone know the syntax for this? I've been looking everywhere and all I can find is C++ code for this. I'm trying to password protect an excel file programatically using the System.IO.Packaging namespace.
有谁知道这个的语法?我到处找,我能找到的只是 C++ 代码。我正在尝试使用 System.IO.Packaging 命名空间以编程方式对 Excel 文件进行密码保护。
Any ideas?
有任何想法吗?
Additional notes:
补充说明:
I'm NOT using the Excel interop--but instead the System.IO.Packaging namespace to encrypt and password protect the excel file.
我没有使用 Excel 互操作,而是使用 System.IO.Packaging 命名空间来加密和密码保护 Excel 文件。
采纳答案by Colin Pickard
If you want an Excel password all you need is something like this:
如果你想要一个 Excel 密码,你只需要像这样:
using Microsoft.Office.Interop.Excel
//create your spreadsheet here...
WorkbookObject.Password = password;
WorkbookObject.SaveAs("spreadsheet.xls")
This requires Excel to be installed.
这需要安装 Excel。
That's nothing to do with System.IO.Packaging
of course, so you might need to restate your question...
这System.IO.Packaging
当然无关,所以你可能需要重申你的问题......
回答by Dirk Vollmar
It's not possible using System.IO.Packaging
. You will have to use Microsoft.Office.Interop.Excel
using the Worksheet.SaveAs
method. This requires Excel being installed on your target system.
不可能使用System.IO.Packaging
. 您将不得不使用Microsoft.Office.Interop.Excel
usingWorksheet.SaveAs
方法。这需要在您的目标系统上安装 Excel。
回答by user110190
You will have to use the SaveAs method on the Worksheet. It has a parameter to set a password. Here is an example in VB which can be converted to C#
您必须在工作表上使用 SaveAs 方法。它有一个参数来设置密码。这是VB中的一个例子,可以转换为C#
回答by Chandrasekhar Telkapalli
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)
{
}
}
}