我在服务器生成EXCEL文件,当执行下面的代码时,在服务器上就会生成开一个EXCEL.EXE进程
我想在操作完成时把这个进程关闭,该怎么关闭啊!! Excel.ApplicationClass myExcel = new Excel.ApplicationClass(); Excel._Workbook xBk; //工作薄
Excel._Worksheet xSt; //工作Sheet
:
:
:
xBk.Close(false, "", null);
myExcel.Quit();
xBk = null;
xSt = null;
myExcel = null;
GC.Collect(); //还是不能关闭啊!!大侠指点啊!!
我想在操作完成时把这个进程关闭,该怎么关闭啊!! Excel.ApplicationClass myExcel = new Excel.ApplicationClass(); Excel._Workbook xBk; //工作薄
Excel._Worksheet xSt; //工作Sheet
:
:
:
xBk.Close(false, "", null);
myExcel.Quit();
xBk = null;
xSt = null;
myExcel = null;
GC.Collect(); //还是不能关闭啊!!大侠指点啊!!
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
protected void Button1_Click(object sender, EventArgs e)
{
Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Open("d:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
System.GC.Collect();
1.使用单例模式,保证服务器上只有一个excel进程
2. 用kill process来结束进程,但是会造成服务器上别的人就别想用excel了.
xSt = null;
myExcel = null;
myExcel.Quit(); 顺序问题吧
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B317109
{
workbook.Close(false,fullFileName,false);
} //Release Excel
if(xSt!= null)
{
Marshal.ReleaseComObject(xSt);
xSt= null;
}
if(xBk!= null)
{
Marshal.ReleaseComObject(xBk);
xBk= null;
}
if(myExcel != null)
{
Marshal.ReleaseComObject(myExcel);
myExcel = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
/// <summary>
/// 如何杀死word,excel等进程,下面的方法可以直接调用
/// </summary>
/// <param name="processName">进程名称</param>
public void KillProcess(string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process(); //得到所有打开的进程
try
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
{
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
{
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if (p.ProcessName.IndexOf(strName) >= 0)
{
p.Kill();
}
}
} static void Rest()
{
Rest(10);
}
static void Rest(int second)
{
System.Threading.Thread.Sleep(second * 1000);
}
比如你的是
ExcelOut(...)
{
.....
myExcel.quit()
}
那么你调用这个过程的时候在写一个
ExcelOut1(...)
{
ExcelOut(....);
GC.Collect();
}
就可以关闭Excel进程了
这个参数直接给“EXCEL.EXE”就可以了吗???
这样真的可以吗?