关闭Excel进程

今天发现导入数据时打开了很多的Excel进程,上网看了会,都说方法外垃圾回收和杀进程。详细试了试,我的代码似乎只需要Quit就行。

代码
using Excel = Microsoft.Office.Interop.Excel;

Excel.Application m_objExcel
= null;
Excel._Workbook m_objBook
= null;
Excel.Sheets m_objSheets
= null;
Excel._Worksheet m_objSheet
= null;
object m_objOpt = System.Reflection.Missing.Value;
try
{
m_objExcel
= new Excel.Application();
m_objBook
= m_objExcel.Workbooks.Open(filePath, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

m_objSheets
= (Excel.Sheets)m_objBook.Worksheets;
m_objSheet
= (Excel._Worksheet)(m_objSheets.get_Item(1));
Excel.Range myRange
= null;
for (int i = startRow; i < maxRow; i++) //循环填充数据
{
//读取主值
//Excel.Range range = m_objSheet.get_Range("A" + i.ToString(), "C" + i.ToString());
//System.Array myvalues = (System.Array)range.Cells.Value2;
myRange = (Excel.Range)m_objSheet.Cells[i, valIndex];
string val = string.Format("{0}", myRange.Value2);
if (string.IsNullOrEmpty(val))//空白的值不使用
continue;
myRange
= (Excel.Range)m_objSheet.Cells[i, keyIndex];
string key = string.Format("{0}", myRange.Value2);
if (this.excelContent.ContainsKey(key))
{
Console.WriteLine(
string.Format("重复编码。行{0}编码{1}", i, key));
continue;
}
this.excelContent.Add(key, val);
}
}
catch(Exception ex)
{
Console.WriteLine(
"取原始信息出错:" + ex.Message);
}
finally
{
m_objExcel.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);//这个象没有效果
//System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
//int generation = System.GC.GetGeneration(m_objExcel);
//GC.Collect(generation);
}

 

WinForm程序,excel版本12.0.0.0

posted @ 2010-11-29 17:13  生命体验之kevin-Y  阅读(265)  评论(0编辑  收藏  举报