2016-01-29 19 views
5

Tôi có một ứng dụng C# và nó cần tạo một ứng dụng excel & rồi mở một sổ làm việc. Vấn đề là tôi cần một addinn Bloomberg để được nạp khi excel mở ra. Cách duy nhất tôi đã tìm thấy là trong bài viết này working example.bắt đầu ứng dụng excel với các addins từ C# application

Điều này thực sự bắt đầu nổi trội và có thể sử dụng các chức năng của Bloomberg. Tuy nhiên tôi đã tự hỏi nếu có một cách để đúc myXl để xlApp nơi xlApp là loại Microsoft.Office.Interop.Excel.Application?

var myXl = Process.Start("excel.exe"); 

Lý do là tôi có thư viện có chức năng hữu ích mà tôi muốn sử dụng nhưng cần một tham số kiểu Microsoft.Office.Interop.Excel.Application. Làm thế nào để tôi làm điều này?

+2

Thêm tham chiếu vào thư viện Excel và thay vì var myXL bạn có thể tạo một đối tượng sổ làm việc Excel.Application myXl = New Excel.Application(); – Sorceri

+0

Vấn đề với mặc dù là addberg bloomberg không tải bằng cách sử dụng phương pháp đó – mHelpMe

+0

Bạn đã thấy bài đăng này: http: // stackoverflow.com/questions/213375/loading-addins-when-excel-is-instantiated-programmatically –

Trả lời

0

Thêm một tham chiếu đến thư viện Excel và thay vì var myXL bạn có thể tạo một đối tượng workbook

Excel.Application myXl = New Excel.Application(); 

Sau đó, bạn chỉ cần phải tự tải các add-in.

 foreach (Excel.AddIn item in myXl.AddIns) 
     { 
      if (item.Name.Equals("BLOOMBERG ADDIN NAME")) 
      { 
       item.Installed = true; 
      } 
     } 
3

Bạn có thể tự động hóa Excel từ ứng dụng bên ngoài. Xem How to automate Microsoft Excel from Microsoft Visual C#.NETC# app automates Excel (CSAutomateExcel) để biết thêm thông tin.

Lớp Application cung cấp các thuộc tính sau để truy cập vào add-ins:

  • AddIns - trả về một bộ sưu tập AddIns đại diện cho tất cả các add-in được liệt kê trong Add-Ins hộp thoại (Add-Ins lệnh trên tab Nhà phát triển); Add-in XLL.
  • COMAddIns - trả về bộ sưu tập COMAddIns cho Microsoft Excel, đại diện cho các trình bổ sung COM hiện được cài đặt.

Vì vậy, nếu bạn cần đảm bảo rằng bổ trợ COM được bật, bạn cần sử dụng thuộc tính ComAddins của lớp Ứng dụng.

1

Bạn có thể sử dụng đoạn mã sau:

này sẽ bắt đầu Excel, sau đó đi qua tất cả Workbooks đăng ký trong Object Bảng Chạy để tìm một trong đó chạy trong quá trình chỉ mới bắt đầu. Để làm điều này, nó nhận được quá trình id của cửa sổ xử lý của bảng tính và so sánh nó với id của quá trình chỉ mới bắt đầu.

Việc tìm kiếm trong Bảng đối tượng đang chạy được lặp lại nhiều lần trong khoảng thời gian chờ vì Excel có thể cần một chút thời gian để đăng ký với ROT sau khi bắt đầu. Bạn có thể cần phải tăng maxAttemptswaitTimeMS trên các máy tính chậm hơn.

Nếu tìm đúng Sổ làm việc, nó sẽ được trả lại. Trong ví dụ này, tôi sẽ viết "hello world" vào ô đầu tiên của cá thể ứng dụng Excel.

private void button1_Click(object sender, EventArgs e) 
{ 
    Microsoft.Office.Interop.Excel.Application excelApplication = StartExcel(); 

    if (excelApplication != null) 
    { 
     excelApplication.ActiveCell.Value = "Hello World"; 
    } 
} 

[DllImport("user32.dll", SetLastError = true)] 
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); 

[DllImport("ole32.dll")] 
private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot); 

private Microsoft.Office.Interop.Excel.Application StartExcel() 
{ 
    // Maximum number of attempts to look for started Excel Application 
    const int maxAttempts = 3; 
    // Number of milliseconds to wait between attempts to look for started Excel Application 
    const int waitTimeMS = 200; 

    Microsoft.Office.Interop.Excel.Application result = null; 

    // Start Excel 
    var process = Process.Start("Excel.exe"); 
    process.WaitForInputIdle(); 

    // Try to find started Excel Application 

    int currentAttempt = 1; 

    while ((result == null) && (currentAttempt <= maxAttempts)) 
    { 
     // Wait between attempts 
     if(currentAttempt != 1) 
     { 
      Thread.Sleep(waitTimeMS); 
     } 

     // List all running Excel automation objects and find the one with the same process id 
     IRunningObjectTable lRunningObjectTable = null; 
     IEnumMoniker lMonikerList = null; 

     try 
     { 
      // Query Running Object Table 
      if (GetRunningObjectTable(0, out lRunningObjectTable) == 0 && lRunningObjectTable != null) 
      { 

       // List Monikers 
       lRunningObjectTable.EnumRunning(out lMonikerList); 

       // Start Enumeration 
       lMonikerList.Reset(); 

       // Array used for enumerating Monikers 
       IMoniker[] lMonikerContainer = new IMoniker[1]; 

       IntPtr lPointerFetchedMonikers = IntPtr.Zero; 

       // foreach Moniker 
       while (lMonikerList.Next(1, lMonikerContainer, lPointerFetchedMonikers) == 0) 
       { 
        object lComObject; 
        lRunningObjectTable.GetObject(lMonikerContainer[0], out lComObject); 

        // Check the object is an Excel workbook 
        if (lComObject is Microsoft.Office.Interop.Excel.Workbook) 
        { 
         Microsoft.Office.Interop.Excel.Workbook lExcelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)lComObject; 

         // Get the Process ID for the Window Handle 
         uint processId; 
         GetWindowThreadProcessId(new IntPtr(lExcelWorkbook.Application.Hwnd), out processId); 

         if (processId == process.Id) 
         { 
          // Correct automation object found, return Application 
          result = lExcelWorkbook.Application; 
          break; 
         } 
        } 
       } 
      } 
     } 
     finally 
     { 
      // Release ressources 
      if (lRunningObjectTable != null) Marshal.ReleaseComObject(lRunningObjectTable); 
      if (lMonikerList != null) Marshal.ReleaseComObject(lMonikerList); 
     } 

     currentAttempt++; 
    } 


    return result; 
} 
Các vấn đề liên quan