카테고리 없음

[씨#] Excel을 통해 WinForm 열기

행복을전해요 2021. 1. 28. 22:26

Excel 파일에서 C # -Exe를 시작하고 다음과 같이 VBA를 통해 Excel 파일의 파일 이름을 전달할 수 있습니다.

Sub StartCSharpExe()
    Shell "<full path to your exe-file> " + ActiveWorkbook.FullName, 1
        Application.Quit //optional to close Excel
        End Sub
        

그런 다음 C # -Program에서 Excel 파일의 파일 이름을 다음과 같은 명령 매개 변수로 읽을 수 있습니다.

   private void GetCommands()
    {
            String[] arrCommands = Environment.GetCommandLineArgs();
                    foreach (String command in arrCommands)
                            {
                                        MessageBox.Show(command); // just for debugging purpose / if you'd like to see all parameters
                                                    // get the excel-file-name and open it...
                                                            }
                                                                }
                                                                

아마도 엑셀 파일은 아직 열려있을 수 있으므로 읽기 전용 일 수
있지만 테스트 하지는 않았지만 이것이 갈 길입니다.

-------------------

여기에 전체 솔루션을 게시 할 것입니다. 사람들에게 유용 할 것이라고 생각하기 때문입니다. 다음 코드를 사용하여 제안 된
대로 Excel에서 양식을 호출합니다 user1567896.

Sub StartCSharpExe()
    Shell "<full path to your exe-file> " + ActiveWorkbook.FullName, 1
        Application.Quit //optional to close Excel
        End Sub
        

그런 다음 Excel 파일을 Excel 추가 기능 파일 (.xla)로 저장합니다.
그 후 (Excel에서) 다음 방법으로 Developer Tab->Add-ins->[browse to your .xla file]->OK
해당 추가 기능을 추가 할 수 있습니다 Quick Access Toolbar.
File->Options->Quick Access Toolbar->[Choose commands from:]Macros->YourAddin.xla

c # 메서드는 다음과 유사해야하며 양식 초기화에서 호출되어야합니다.

using Excel = Microsoft.Office.Interop.Excel;
public static void LoadExcelFile()
{
    Excel.Application EXCEL_FILE = new Excel.Application();
        EXCEL_FILE = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
        }
        

참고 : 몇 개의 창을 열어서 테스트했으며 GetActiveObject("Excel.Application")항상 Excel을 통해 WinForm을 호출하는 창을 반환했습니다.

도움이 되었기를 바랍니다.



출처
https://stackoverflow.com/questions/22039791