在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:07:45  来源:igfitidea点击:

Password Protecting an Excel file in C#

c#excelpasswordspassword-protection

提问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.Packagingof 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.Excelusing the Worksheet.SaveAsmethod. This requires Excel being installed on your target system.

不可能使用System.IO.Packaging. 您将不得不使用Microsoft.Office.Interop.ExcelusingWorksheet.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#

http://www.codeproject.com/KB/office/Excel_Security.aspx

http://www.codeproject.com/KB/office/Excel_Security.aspx

回答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)
        {

        }

    }
}