我是通过这样的方式实现的的但是 执行完毕以后 excel文件被加了锁 进程里还有一个Excel的进程怎么解决呢
using System;
using System.Collections.Generic;
using System.Text;using Microsoft.Office.Core;
using ExcelAPI=Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace ExcelOrderReader
{
public class ExcelDocument
{
string filePath; private ExcelAPI.Workbook workbook;
private ExcelAPI.Application app; public ExcelDocument(string filePath)
{
this.filePath = filePath;
} public ExcelAPI.Workbook OpenDocument()
{
return this.openDocument(filePath);
} private ExcelAPI.Workbook openDocument(string fileName)
{
object MissingValue = Type.Missing;
this.app = new ExcelAPI.Application();
workbook = app.Workbooks.Open(fileName, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
return workbook;
} public void CloseDocument()
{
try
{
if (workbook!=null)
{
for (int i = 1; i < workbook.Sheets.Count; i++)
{
ExcelAPI.Worksheet workSheet = (ExcelAPI.Worksheet)workbook.Sheets[i];
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
workbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null; if (app != null)
{
app.Workbooks.Close();
app.Quit();
KillSpecialExcel(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null; }
}
}
catch (Exception)
{
throw;
}
finally
{
GC.Collect();
} } #region 结束EXCEL.EXE进程的方法
/// <summary>
/// 结束EXCEL.EXE进程的方法
/// </summary>
/// <param name="m_objExcel">EXCEL对象</param>
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); public void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
{
try
{
if (m_objExcel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion ~ExcelDocument()
{
CloseDocument();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;using Microsoft.Office.Core;
using ExcelAPI=Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace ExcelOrderReader
{
public class ExcelDocument
{
string filePath; private ExcelAPI.Workbook workbook;
private ExcelAPI.Application app; public ExcelDocument(string filePath)
{
this.filePath = filePath;
} public ExcelAPI.Workbook OpenDocument()
{
return this.openDocument(filePath);
} private ExcelAPI.Workbook openDocument(string fileName)
{
object MissingValue = Type.Missing;
this.app = new ExcelAPI.Application();
workbook = app.Workbooks.Open(fileName, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
return workbook;
} public void CloseDocument()
{
try
{
if (workbook!=null)
{
for (int i = 1; i < workbook.Sheets.Count; i++)
{
ExcelAPI.Worksheet workSheet = (ExcelAPI.Worksheet)workbook.Sheets[i];
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
workbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null; if (app != null)
{
app.Workbooks.Close();
app.Quit();
KillSpecialExcel(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null; }
}
}
catch (Exception)
{
throw;
}
finally
{
GC.Collect();
} } #region 结束EXCEL.EXE进程的方法
/// <summary>
/// 结束EXCEL.EXE进程的方法
/// </summary>
/// <param name="m_objExcel">EXCEL对象</param>
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); public void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
{
try
{
if (m_objExcel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
throw;
}
}
#endregion ~ExcelDocument()
{
CloseDocument();
}
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject (各个对象);