使用 C# 密码保护 Excel 文件

发布于 2024-07-21 11:08:07 字数 209 浏览 8 评论 0原文

有谁知道这个的语法吗? 我一直在到处寻找,但我能找到的只是这方面的 C++ 代码。 我正在尝试使用 System.IO.Packaging 命名空间以编程方式保护 Excel 文件。

有任何想法吗?

附加说明:

我没有使用 Excel 互操作性,而是使用 System.IO.Packaging 命名空间来加密和密码保护 Excel 文件。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总以为 2024-07-28 11:08:07

如果您想要 Excel 密码,您只需要如下所示:

using Microsoft.Office.Interop.Excel

//create your spreadsheet here...

WorkbookObject.Password = password;
WorkbookObject.SaveAs("spreadsheet.xls")

这需要安装 Excel。

当然,这与 System.IO.Packaging 无关,因此您可能需要重申您的问题......

If you want an Excel password all you need is something like this:

using Microsoft.Office.Interop.Excel

//create your spreadsheet here...

WorkbookObject.Password = password;
WorkbookObject.SaveAs("spreadsheet.xls")

This requires Excel to be installed.

That's nothing to do with System.IO.Packaging of course, so you might need to restate your question...

﹏半生如梦愿梦如真 2024-07-28 11:08:07

使用 System.IO.Packaging 是不可能的。 您必须使用 Worksheet.SaveAs 方法。 这需要在目标系统上安装 Excel。

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.

十秒萌定你 2024-07-28 11:08:07

您必须在工作表上使用 SaveAs 方法。 它有一个参数来设置密码。 以下是 VB 中的示例,可以将其转换为 C#

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

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#

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

清浅ˋ旧时光 2024-07-28 11:08:07
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)
        {

        }

    }
}
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)
        {

        }

    }
}
小清晰的声音 2024-07-28 11:08:07

从 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();
        

Download Microsoft.Office.Interop.Excel from NuGet Package. Password protect Excel file using c# code below

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();
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文