2009-05-27 52 views
6

Có ai biết cú pháp này không? Tôi đã tìm kiếm ở khắp mọi nơi và tất cả những gì tôi có thể tìm thấy là mã C++ cho việc này. Tôi đang cố gắng để bảo vệ mật khẩu một tập tin excel programatically bằng cách sử dụng không gian tên System.IO.Packaging.Mật khẩu Bảo vệ tệp Excel trong C#

Bất kỳ ý tưởng nào?

Ghi chú thêm:

tôi không sử dụng các interop Excel - nhưng thay vì không gian tên System.IO.Packaging để mã hóa và mật khẩu bảo vệ file excel.

+0

Vì vậy, bạn đang cố gắng để tạo ra một số loại mật khẩu bảo vệ file zip có nghĩa là câu hỏi của bạn là không cụ thể cho excel? – VVS

+2

Như tôi đã hiểu, ông đề cập đến tính năng "Lưu với mật khẩu" của Excel có thể được sử dụng thông qua Excel OM. –

Trả lời

7

Nếu bạn muốn một mật khẩu Excel mọi thứ bạn cần là một cái gì đó như thế này:

using Microsoft.Office.Interop.Excel 

//create your spreadsheet here... 

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

Điều này đòi hỏi Excel được cài đặt.

Đó là không có gì để làm với System.IO.Packaging tất nhiên, vì vậy bạn có thể cần phải xác định lại câu hỏi của bạn ...

0

Không thể sử dụng System.IO.Packaging. Bạn sẽ phải sử dụng Microsoft.Office.Interop.Excel bằng phương pháp Worksheet.SaveAs. Điều này yêu cầu Excel được cài đặt trên hệ thống đích của bạn.

+0

Đây là liên kết giải thích điều này cho những người quan tâm: https://social.msdn.microsoft.com/Forums/en-US/7021768e-4562-43dd-9d5e-89c10970bf33/create-password-protected-excel-sheet-with- ooxml? forum = os_specifications – MikeTeeVee

1

Bạn sẽ phải sử dụng phương thức SaveAs trên Bảng tính. Nó có một tham số để thiết lập một mật khẩu. Dưới đây là một ví dụ trong VB mà có thể được chuyển đổi sang C#

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

+0

Mặc dù Excel (và tự động hóa COM nói chung) là cách dễ dàng hơn trong VB hơn trong C# (3.0). –

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

     } 

    } 
} 
+0

sử dụng Excell Interop, tuy nhiên, điểm then chốt ở đây là Workbook.WritePassword = "somepassword", đây là cách chúng ta cần gán. –

Các vấn đề liên quan