111,092
社区成员




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)
{
}
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));
发现出现 WorkSheet = WorkBook.Sheets[1];
或者 Excel.Worksheet WorkSheet = WorkBook.Sheets.get_Item(1);
就不能关闭进程,不知道为什么?
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;
}