Execute excel as another process

Published Jul 23, 2015 | Updated Sep 17, 2022 | 0 comments

Excel 의 경우 여러개의 파일을 동일한 Process 가 처리하기 때문에

이미 다른 파일이 열려있는 경우 WaitForExit 가 먹히지 않음

그래서 별도 Process 로 Excel 을 실행시킴

void ExecuExcelAsAnotherProcess()
{
    string excelProcessPath = "";
    string fileName = @"C:\Users\Administrator\Downloads\Test.xlsx_";
    try
    {
        this.Cursor = Cursors.WaitCursor;
 
        excelProcessPath = GetExcelProcessPath();
 
        string cmd = string.Format(""{0}" /x "{1}"", excelProcessPath, fileName);
 
        System.Diagnostics.ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(cmd);
        psInfo.CreateNoWindow = false;
        psInfo.UseShellExecute = false;
        using (System.Diagnostics.Process ps = System.Diagnostics.Process.Start(psInfo))
        {
            ps.EnableRaisingEvents = true;
            ps.WaitForExit();
        }
    }
    finally
    {
        this.Cursor = Cursors.Default;
    }
}
 
string GetExcelProcessPath()
{
    dynamic app = null;
    try
    {
        Type excelType = Type.GetTypeFromProgID("Excel.Application");
        app = Activator.CreateInstance(excelType);
        app.Visible = true;
        app.DisplayAlerts = true;
 
        return Path.Combine(app.Path, "excel.exe");
    }
    finally
    {
        if (app != null)
        {
            app.Quit();
        }
        NAR(app);
    }
 
}
 
void NAR(object obj)
{
    try
    {
        if (obj != null)
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
    }
    catch { }
    finally
    {
        obj = null;
    }
}

TAG INFORMATION

Learn more on this topic

Related Blog Posts

Join in the conversation

Leave a Comment

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

무료 온라인 전광판

전광판

텍스트를 입력하고 텍스트 효과 및 배경효과 를 변경해서 전체화면으로 표시할 수 있는 전광판 용도로 사용하실 수 있습니다. 각종 스포츠 및 공연 관람시 응원 용도로 사용이 가능합니다.

Carousel

여러개의 슬라이드를 추가하여 프레젠테이션 및 이미지 슬라이드 용도로 사용하실 수 있습니다. 브라우저가 포함된 IT 기기로 큰 모니터에 연결하여 매장 내 공지사항 및 메뉴소개를 이미지로 표시할 수 있습니다.

Pin It on Pinterest

Shares
Share This

Share This

Share this post with your friends!

Shares