4

We currently create Excel spreadsheets on our server which are then mailed to customers. We now need to password protect these files. Is it possible to programmatically add Excel password protection to these files?

I would prefer not to install Excel on the server and use a form automation to do this.

I am a C#/.net developer, but any language/method to do this would be appreciated

1

8 Answers 8

3

You can also check out "HiveLink" at http://hivelink.io. It's a service that allows you to protect your Excel spreadsheets by creating a lightweight version of your spreadsheet for your users. The lightweight version just has the input interface, all the calculations removed, and the output results interface. You can send invitations to each user, they can download the lightweight spreadsheet, enter their inputs and receive the results shortly after. HiveLink connects the spreadsheets securely taking the input from the user and running it through the original spreadsheet and returning the results.

Sign up to request clarification or add additional context in comments.

Comments

2

Thanks for you replies. It seems that I cannot easily do what I asked. So I had to use automation. !st I added a reference to the excel interop then the following code did what I needed. (Runs from command line with 2 params filename and password)

    static void Main(string[] args)
    {
        if (args.Length != 2)
            return;

        string filename = args[0];
        string password = args[1];

        if (!File.Exists(filename))
            return;

        Excel.Application oexcel;
        Excel.Workbook obook;

        oexcel = new Excel.Application();
        oexcel.DisplayAlerts = false;

        obook = oexcel.Workbooks.Open(filename, 0, false, 5, "", "", true, System.Reflection.Missing.Value, "\t", false, false, 0, true, 1, 0);

        try
        {
            obook.SaveAs(filename, Password: password, ConflictResolution: XlSaveConflictResolution.xlLocalSessionChanges);
        }
        finally
        {
            obook.Close();
            oexcel.Quit();
        }
    }

Comments

1

I would suggest using a proven encryption package. (eg PGP) The password protection provided by excel is not nearly as strong.

5 Comments

We already use PGP for some files, however some users have specified that they just want a password protected excel file.
@user406225 - And why do they want such a file? If it is to provide data security then their request is invalid. They will get no security from password protected excel except for some version (2002, 2003?) and only if the options are set exactly right. There are huge security holes in the encryption and protection used by office documents.
Hi Hogan, do you have some reference for the claim about bad security in Excel password protection? AFAIK Excel (from at least 2007 and forward) uses AES 256 for encrypting files (Office uses the Office CRyptoAPI), but even that can protect against short passwords.
@Jesper - As I understand it these documents can be broken if multiple documents can be compared. The older stuff is clearly broken and the default is to use compatibility. As MS says it is not intended to secure a document "merely a functionality to prevent accidental changes of a document."
Hi Hogan, you are talking about two different things. One is the legacy Office apps that used (AFAIR) RC4 for encryption and that is clearly too weak to use today. But as I said, today Office uses AES 256 for encryption and there are no known attacks on that - apart from dictionary-attacks, of course. The "mereley ... to prevent accidental" - that part deals with "document protection" that allows an app to display e.g. an "unlock to edit"-dialogue to the user. It does not protect the document itself. The algorithm used by Office to hash password for "unlock" is very weak, btw.
0

Edit: Misunderstood question, disregard.

The simplest way I would solve this is by compressing with encryption.

1 Comment

You can add a password to ecel files, so that when you attempt to open them with excel you are prompted to enter them password. This is what the user wants us to do
0

Anything I could think of would require Excel on the server and use Excel Automation.

However, an alternative would be SharpZipLib and put the Excel file in a password protected ZIP file. Windows XP and better ship with a default handler for ZIP that can handle passwords, so the net effect is a password protected file, they just need to extract it first, but no extra software should be required. The only negative is a Password Protected Zip uses an algorithm that is flawed, and I don't believe the Windows Shell Extension for ZIPs can understand AES yet.

Comments

0

You are not set for an easy task, but regardless, you should take a look at the article "Document Encryption in Office 2007 with Open XML" by Gray Knowlton at http://blogs.technet.com/b/gray_knowlton/archive/2008/09/02/document-encryption-in-office-2007-with-open-xml.aspx

Also check out the thread on MSDN on http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/c48048a3-4013-419f-b612-3d971acb1919

I hope this helps a bit.

:o)

Comments

0

Haven't tried it myself, but http://xlsgen.arstdesign.com/intro.html Looks like it might work for you (especially the details on http://xlsgen.arstdesign.com/core/locked.html)

As a corollary, on a project we were working on a couple years ago, we had a system that would build Excel files using interop libraries. In dev it worked fine, in test it worked fine, but blew up on staging every time. Finally tracked it down to MS NOT supporting the interop assemblies on the Server environment. Our staging was Server 2008, test was 2003, so your mileage may very if you are going with interop on a server.

Comments

0

I don't know if this helps or not but I had to do something quite similar recently. I had to create workbooks using OpenXML then password protect them afterwards.

     Public Sub CreateWorkBookPassword(WorkBookPath As String, password As String)

        Dim excelApplication As New Application()
        Dim excelWorkbook = excelApplication.Workbooks.Add(WorkBookPath)

        Try
            excelApplication.DisplayAlerts = False
            excelWorkbook.SaveAs(WorkBookPath, XlFileFormat.xlOpenXMLWorkbook, password)

        Finally
            excelWorkbook.Close()
            excelApplication.Quit()

            'Close all excel processes that may be running
            Dim excelProcesses() As Process = Process.GetProcessesByName("EXCEL")
            For Each Process As Process In excelProcesses
                Process.Kill()
                Exit For
            Next
        End Try
    End Sub

Usage

 CreateWorkBookPassword("PathToExistingWorkbook", "yourPassword")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.