Password Protecting an Excel file in C#
Asked Answered
F

5

8

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.

Fard answered 27/5, 2009 at 14:25 Comment(2)
So you're trying to create some sort of a password protected zip file which means your question is not specific to excel?Trescott
As I understood he refers to the "Save with password" feature of Excel which can be used via the Excel OM.Harriet
N
8

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...

Nicolasanicolau answered 27/5, 2009 at 14:36 Comment(1)
This solution worked. But this will require to install Microsoft office in servers wherever this code will be running. As there are many servers in my case, customer is not ready pay for Microsoft office in each server. Is there any other way to set password in Excel file without having to install Microsoft office?Recollected
H
1

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.

Harriet answered 27/5, 2009 at 14:31 Comment(1)
Here's a link explaining this for those interested: social.msdn.microsoft.com/Forums/en-US/…Advertence
T
1

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

Telegraphone answered 27/5, 2009 at 14:33 Comment(1)
Although Excel (and COM automation in general) is way easier in VB than in C# (3.0).Harriet
D
1
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)
        {

        }

    }
}
Deanadeanda answered 27/6, 2017 at 19:38 Comment(1)
using Excell Interop, but however keypoint here is Workbook.WritePassword="somepassword", this is the way we need to assign.Deanadeanda
P
0

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();
        
Palla answered 28/2, 2022 at 23:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.