问题描述
我打开电子表格一些代码,读取一些值,然后关闭表。我需要为多个文件做到这一点。我遇到的问题是Excel的应用程序实例没有退出,所以当我运行过程的几个文件,我结束了几个EXCEL.EXE进程在运行。任何想法我如何获得Excel中关闭?
静态无效ParseFile(字符串文件)
{
试
{
的System.Console.WriteLine(分析:+文件);
Excel.Application的Excel =新Excel.Application();
Excel.Workbook WB = excel.Workbooks.Open(文件);
Excel.Worksheet WS = wb.Worksheets [1];
的for(int i = 2; I< 27;我++)
{
日志(ws.Cells [1,1]。文本);
}
wb.Close(假);
excel.Quit();
的Excel = NULL;
WS = NULL;
WB = NULL;
}
赶上(异常前)
{
日志(ex.Message);
}
}
------更新12月11日/ 12 --------还是退出Excel实例中打开-------
静态无效ParseFile(串文件)
{
试
{
日志(分析:+文件);
Excel.Application的Excel =新Excel.Application();
Excel.Workbook WB = excel.Workbooks.Open(文件);
Excel.Worksheet WS = wb.Worksheets [1];
//做一些东西在这里
wb.Close(假);
excel.Quit();
GC.Collect的();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(WS);
Marshal.FinalReleaseComObject(WB);
Marshal.FinalReleaseComObject(EXCEL);
的Excel = NULL;
WS = NULL;
WB = NULL;
System.GC.Collect();
}
赶上(异常前)
{
日志(ex.Message);
}
}
端在杀死该进程,这是只有工作的事情。
函数[DllImport(user32.dll中)]
私人静态外部UINT GetWindowThreadProcessId(IntPtr的的HWND,UINT出lpdwProcessId);
///<总结>试图寻找并通过的hWnd到进程的主窗口中杀死进程< /总结>
///&下; PARAM NAME =的hWnd>句柄的进程的主窗下; /参数>
///<退货和GT;如果为true过程被发现身亡。假,如果过程不被的hWnd,或者如果它不能被杀死找到< /回报>
公共静态布尔TryKillProcessByMainWindowHwnd(INT的hWnd)
{
UINT进程ID;
GetWindowThreadProcessId((IntPtr的)的HWND,出进程ID);
如果(进程ID == 0)返回false;
试
{
Process.GetProcessById((int)的进程ID).Kill();
}
赶上(ArgumentException的)
{
返回false;
}
赶上(异常前)
{
返回FALSE;
}
返回真;
}
静态无效ParseFile(字符串文件)
{
试
{
日志(分析:+文件);
Excel.Application的Excel =新Excel.Application();
Excel.Workbook WB = excel.Workbooks.Open(文件);
Excel.Worksheet WS = wb.Worksheets [1];
//做一些东西在这里
wb.Close(假);
INT的hWnd = excel.Application.Hwnd;
excel.Quit();
GC.Collect的();
GC.WaitForPendingFinalizers();
GC.Collect的();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(WS);
Marshal.FinalReleaseComObject(WB);
Marshal.FinalReleaseComObject(EXCEL);
的Excel = NULL;
WS = NULL;
WB = NULL;
GC.Collect的();
GC.WaitForPendingFinalizers();
GC.Collect的();
GC.WaitForPendingFinalizers();
TryKillProcessByMainWindowHwnd(HWND);
}
赶上(异常前)
{
日志(ex.Message);
}
}
I have some code that opens a spreadsheet, reads some values, and then closes the sheet. I need to do this for multiple files. The problem I'm having is that the Excel application instance is not exiting, therefore when I run my process for several files, I end up with several excel.exe processes running. Any ideas how I get Excel to close?
static void ParseFile(string file)
{
try
{
System.Console.WriteLine("parsing:" + file);
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.Workbooks.Open(file);
Excel.Worksheet ws = wb.Worksheets[1];
for (int i = 2; i < 27; i++)
{
log(ws.Cells[i, 1].Text);
}
wb.Close(false);
excel.Quit();
excel = null;
ws = null;
wb = null;
}
catch (Exception ex)
{
log(ex.Message);
}
}
------Update 12/11/12--------Still leaving excel instances open-------
static void ParseFile(string file)
{
try
{
log("parsing:" + file);
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.Workbooks.Open(file);
Excel.Worksheet ws = wb.Worksheets[1];
//do some stuff here
wb.Close(false);
excel.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(ws);
Marshal.FinalReleaseComObject(wb);
Marshal.FinalReleaseComObject(excel);
excel = null;
ws = null;
wb = null;
System.GC.Collect();
}
catch (Exception ex)
{
log(ex.Message);
}
}
Ended up killing the processes, that was the only thing that worked.
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
/// <summary> Tries to find and kill process by hWnd to the main window of the process.</summary>
/// <param name="hWnd">Handle to the main window of the process.</param>
/// <returns>True if process was found and killed. False if process was not found by hWnd or if it could not be killed.</returns>
public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
uint processID;
GetWindowThreadProcessId((IntPtr)hWnd, out processID);
if (processID == 0) return false;
try
{
Process.GetProcessById((int)processID).Kill();
}
catch (ArgumentException)
{
return false;
}
catch (Exception ex)
{
return false;
}
return true;
}
static void ParseFile(string file)
{
try
{
log("parsing:" + file);
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.Workbooks.Open(file);
Excel.Worksheet ws = wb.Worksheets[1];
//do some stuff here
wb.Close(false);
int hWnd = excel.Application.Hwnd;
excel.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(ws);
Marshal.FinalReleaseComObject(wb);
Marshal.FinalReleaseComObject(excel);
excel = null;
ws = null;
wb = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
TryKillProcessByMainWindowHwnd(hWnd);
}
catch (Exception ex)
{
log(ex.Message);
}
}
这篇关于Excel的互操作COM不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!