开发者

Is it possible to programmatically password protect an excel spreadsheet?

We cur开发者_如何转开发rently 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


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.


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


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


Edit: Misunderstood question, disregard.

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


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.


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)


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.


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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜