Can I programmatically password protect an Excel spreadsheet?

We are currently creating Excel spreadsheets on our server, which are then mailed to clients. Now we need to password protect these files. Is it possible to programmatically add Excel file protection to these files?

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

I am a C # / developer. net, but any language / method for this will be appreciated

+4
source share
9 answers

You can also check out "HiveLink" at http://hivelink.io . This is a service that allows you to protect Excel spreadsheets by creating a light version of your spreadsheet for your users. The light version has only an input interface, all remote calculations and an output results interface. You can send invitations to each user, they can upload a light spreadsheet, enter their inputs and receive results shortly afterwards. HiveLink connects spreadsheets, reliably taking input from the user and running them through the original spreadsheet and returning the results.

+3
source

. , , . .! st excel interop, , . ( 2 )

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

. (, PGP) , excel, .

+1

. , .

- .

0

, , Excel Excel Automation.

SharpZipLib Excel ZIP . Windows XP ZIP, , - , , . - Zip , , , Windows Shell ZIP- AES.

0

, http://xlsgen.arstdesign.com/intro.html , ( http://xlsgen.arstdesign.com/core/locked.html)

, , , , Excel interop. dev , , . , MS NOT, interop . Server 2008, 2003, , .

0

, , - . OpenXML, .

     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

 CreateWorkBookPassword("PathToExistingWorkbook", "yourPassword")
0

   Excel .

You should try GroupDocs.Merger for the .NET API . It allows you to protect / protect Excel spreadsheet regardless of whether Excel is installed on the server or not.

string password = "iamironman";  
Stream openFile = new FileStream(sourceFile, FileMode.Open);  
DocumentResult result = new DocumentHandler().AddPassword(openFile, password);  
Stream documentStream = result.Stream;
var fileStream = File.Create(@"D:/Data/Output." + result.FileFormat);
documentStream.CopyTo(fileStream);
documentStream.Close();

Disclosure: I work as an evangelist developer at GroupDocs.

0
source

All Articles