C#Excel Interop:Excel进程保留在内存中,直到父表单关闭 [英] C# Excel Interop : Excel process remains in memory until parent form closed

查看:90
本文介绍了C#Excel Interop:Excel进程保留在内存中,直到父表单关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private void btnPrintPickList_Click(object sender,EventArgs e)
{
using(var salesRpt = new SalesOrder(CurrentItem()))
{
salesRpt.CreateSpreadSheet();
salesRpt.Dispose();
}
}

我遵循没有2点规则为excel interop

  protected ExcelSheet(bool documentVisible,XlPageOrientation orientation)
{
ExcelApplication = new Application { Visible = documentVisible};
工作簿= ExcelApplication.Workbooks;
WorkBook = WorkBooks.Add(XlSheetType.xlWorksheet);
SheetList = WorkBook.Worksheets;
Orientation = orientation;
WorkSheet =(Worksheet)ExcelApplication.ActiveSheet;
}

public Application ExcelApplication {get;私人集}
public Workbook WorkBook {get;私人集}
公共工作簿工作簿{get;私人集}
public Worksheet WorkSheet {get;私人集}
public Sheet SheetList {get;私人集}
public XlPageOrientation Orientation {get;私人集

dispose方法执行以下操作。

  public void Dispose()
{
for(int i = 1; i< = SheetList.Count; i ++)
{
Marshal.FinalReleaseComObject(SheetList [i]);
}
//Marshal.FinalReleaseComObject(WorkSheet);
Marshal.FinalReleaseComObject(SheetList);
Marshal.FinalReleaseComObject(WorkBook);
WorkBooks.Close();
Marshal.FinalReleaseComObject(WorkBooks);
ExcelApplication.Quit();
Marshal.FinalReleaseComObject(ExcelApplication);
WorkSheet = null;
SheetList = null;
WorkBook = null;
WorkBooks = null;
ExcelApplication = null;
}

在我的测试中,EXCEL.exe进程始终不会从打印Excel电子表格后,任务栏中的当前进程。



我做错了什么?

解决方案

您是否试过调用 GC.Collect()? p>

或者,如果您不想强制立即垃圾收集所有代码,您可以使用使用{} / p>

In my form I am doing something as simple as

private void btnPrintPickList_Click(object sender, EventArgs e)
{
    using (var salesRpt = new SalesOrder(CurrentItem()))
    {
        salesRpt.CreateSpreadSheet();
        salesRpt.Dispose();
    }
}

I have followed the "no 2 dots rule for excel interop".

protected ExcelSheet(bool documentVisible, XlPageOrientation orientation)
{
    ExcelApplication = new Application {Visible = documentVisible};
    WorkBooks = ExcelApplication.Workbooks;
    WorkBook = WorkBooks.Add(XlSheetType.xlWorksheet);
    SheetList = WorkBook.Worksheets;
    Orientation = orientation;
    WorkSheet = (Worksheet) ExcelApplication.ActiveSheet;
}

public Application ExcelApplication { get; private set; }
public Workbook WorkBook { get; private set; }
public Workbooks WorkBooks { get; private set; }
public Worksheet WorkSheet { get; private set; }
public Sheets SheetList { get; private set; }
public XlPageOrientation Orientation { get; private set; }

the dispose method does the following.

    public void Dispose()
    {
        for (int i = 1; i <= SheetList.Count; i++)
        {
            Marshal.FinalReleaseComObject(SheetList[i]);
        }
        //Marshal.FinalReleaseComObject(WorkSheet);
        Marshal.FinalReleaseComObject(SheetList);
        Marshal.FinalReleaseComObject(WorkBook);
        WorkBooks.Close();
        Marshal.FinalReleaseComObject(WorkBooks);
        ExcelApplication.Quit();
        Marshal.FinalReleaseComObject(ExcelApplication);
        WorkSheet = null;
        SheetList = null;
        WorkBook = null;
        WorkBooks = null;
        ExcelApplication = null; 
    }

In my testing, the EXCEL.exe process does not consistently get removed from the current processes in the taskbar once the Excel spreadsheet is printed.

What am I doing wrong?

解决方案

Have you tried calling GC.Collect()?

Alternatively, you could use using{} if you don't want to force an immediate garbage collection of all generations

这篇关于C#Excel Interop:Excel进程保留在内存中,直到父表单关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆