老是提示这个:
文件“d:\excll\bbbc.xls”正由另一进程使用,因此该进程无法访问该文件。
用了以下几种方法都不行:
1.
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Open("d:\\excll\\" + stt + "c.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();
2.
关闭EXCEL进程
xlApp.Workbooks.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
wb = null;
ws = null;
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}}
3.
结束excel 进程代码 excel.DisplayAlerts = false;
work.Close(false, null, false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(work);
work = null;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();都不行···兄弟伙些个帮帮忙呀!!!!!!
文件“d:\excll\bbbc.xls”正由另一进程使用,因此该进程无法访问该文件。
用了以下几种方法都不行:
1.
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Open("d:\\excll\\" + stt + "c.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();
2.
关闭EXCEL进程
xlApp.Workbooks.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
wb = null;
ws = null;
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}}
3.
结束excel 进程代码 excel.DisplayAlerts = false;
work.Close(false, null, false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(work);
work = null;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();都不行···兄弟伙些个帮帮忙呀!!!!!!
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
wb = null;
ws = null;
先关闭相关资源。再操作,sleep延迟
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}}
if (range != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
}
if (range1 != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
range1 = null;
}
if (range2 != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(range2);
range2 = null;
}
if (textBox != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(textBox);
textBox = null;
}
if (workSheet != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
} if (workBook != null)
workBook.Close(null, null, null);
if (excelApp != null) {
excelApp.Workbooks.Close();
excelApp.Quit();
} if (workBook != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
}
if (excelApp != null) {
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
} GC.Collect();
GC.WaitForPendingFinalizers(); }
dcom的方式是不能完全避免不能结束excel进程的问题的如果能限制用户使用excel2007以上的话,建议用http://stephenyy.wordpress.com/2009/11/03/open-xml-sdk-%e9%96%8b%e7%99%bc%e6%8c%87%e5%8d%97%e4%b8%89/