this.Application = new Excel.Application();
this.Application.DisplayAlerts = false;
this.Application.Visible = Visible;
this.WorkBooks = (Excel.Workbooks)this.Application.Workbooks;
this.WorkBook = (Excel._Workbook)(this.WorkBooks.Add(Type.Missing));
this.WorkSheet = (Excel.Worksheet)this.WorkBook.Sheets.get_Item(1);
.....
this.WorkBook.SaveAs(this.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);this.WorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
this.Application.Workbooks.Close(); this.Application.Quit(); if (this.WorkRange != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.WorkRange);
}
if (this.WorkSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.WorkSheet);
}
if (this.WorkBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.WorkBook);
}
if (this.Application != null)
{
//System.Runtime.InteropServices.Marshal.ReleaseComObject(this.Application);
} this.FileName = null;
this.WorkSheetsCount = 0;
this.WorkRange = null;
this.WorkSheet = null;
this.WorkBook = null;
this.WorkBooks = null;
//this.Application = null; GC.Collect();
try
{
if (this.Application != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(this.Application.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
//Kill会跳出 一个 确认的 对话框,同时 进程也没有关闭
}
}
catch (Exception ex)
{ }
Excel 无法关闭。
请教高手~~~~~~~
每操作一次就增加一个Excel 进程。
请教高手~~~~~~~疼苦中
看看是不是多次使用了new Applictaion
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application App = new Excel.Application();
App.DisplayAlerts = false;
App.Visible = false;
Excel.Workbooks WorkBooks = App.Workbooks;
Excel.Workbook WorkBook = WorkBooks.Add(Type.Missing);
Excel.Worksheet WorkSheet = WorkBook.Sheets.get_Item(1);
//Excel.Worksheet WorkSheet = App.ActiveSheet;
/*
经过我千百次的测试,发现Excel.Worksheet WorkSheet = WorkBook.Sheets.get_Item(1);语句引起不能关闭Excel进程! 使用Excel.Worksheet WorkSheet = App.ActiveSheet; 语句的话,则这段代码反复测试都是可以正常关闭进程.
请 各位 验证,并请 回复测试结果 !!! 谢谢~
*/ WorkBook.SaveAs(Server.MapPath("xecc") + "/" + System.Guid.NewGuid().ToString() + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing); try
{
//while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkSheet) > 0) ;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkSheet);
}
catch { }
finally
{
WorkSheet = null;
}
WorkBook.Close(false); try
{
//while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkBook) > 0) ;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkBook);
}
catch { }
finally
{
WorkBook = null;
}
try
{
//while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkBooks) > 0) ;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkBooks);
}
catch { }
finally
{
WorkBooks = null;
}
App.Quit();
try
{
//while (System.Runtime.InteropServices.Marshal.ReleaseComObject(App) > 0) ;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(App);
}
catch { }
finally
{
App = null;
}
try
{
//while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkRange) > 0) ;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkRange);
}
catch { }
finally
{
WorkRange= null;
}
补充:
发现出现 WorkSheet = WorkBook.Sheets[1];
或者 Excel.Worksheet WorkSheet = WorkBook.Sheets.get_Item(1);
就不能关闭进程,不知道为什么?
mySheet = null;
myBook = null;
myExcel.Quit();
myExcel = null;
{
string newpath = Server.MapPath(".") + @"\" + Guid.NewGuid() + ".xls";
_excel.Application app = new _excel.Application();
_excel.Workbooks wbooks = app.Workbooks;
_excel.Workbook wbook = wbooks.Add(System.Reflection.Missing.Value); //VS2003中最好不要如下创建
// _excel.Workbook wbook = app.Workbooks.Add(System.Reflection.Missing.Value); _excel.Worksheet tsheet = (_excel.Worksheet)wbook.ActiveSheet; //为 tsheet.Cells 创建 Range ,方便释放资源
_excel.Range rans = (_excel.Range)tsheet.Cells; //创建ran为了下面赋值时候使用
_excel.Range ran = null; for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
ran = (_excel.Range)rans[i + 1, j + 1];
ran.Value2 = dt.Rows[i][j];
NAR(ran);
//不要如下方式赋值
//tsheet.Cells[i + 1, j + 1] = dt.Rows[i][j];
}
}
NAR(rans);
NAR(tsheet);
//保存信息
wbook.Close(true,newpath, System.Reflection.Missing.Value);
NAR(wbook);
NAR(wbooks);
app.Quit();
NAR(app);
}
/// <summary>
/// 释放资源
/// </summary>
/// <param name="o"></param>
private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}
public class KillExcel
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
/// <summary>
/// 强制关闭当前Excel进程
/// </summary>
public static void Kill(IntPtr intPtr)
{
try
{
Process[] ps = Process.GetProcesses();
int ExcelID = 0;
GetWindowThreadProcessId(intPtr, out ExcelID); //得到本进程唯一标志k
foreach (Process p in ps)
{
if (p.ProcessName.ToLower().Equals("excel"))
{
if (p.Id == ExcelID)
{
p.Kill();
}
}
}
}
catch
{
//不做任何处理
}
}
}调用KillExcel.Kill(new IntPtr(excelApp.Hwnd));