以前写过一个小东西,功能是通过网页上传一个xls文件,后台自动将数据导入到数据库中。至于整个业务逻辑没什么新鲜的,我觉得需要记录下来的是如何在完成数据导入以后释放资源,备忘!
1. 创建Excel application
private
Microsoft.Office.Interop.Excel.Application appExcel;
appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
appExcel.Visible = false ;
appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
appExcel.Visible = false ;
2. 实现IDisposable
public
void
Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
private void Dispose( bool disposing)
{
if ( ! this .bDisposed)
{
if (disposing)
{
if (appExcel != null )
{
appExcel.Quit();
// 释放COM组件,其实就是将其引用计数减1
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
}
}

GC.Collect();
}

}
{
Dispose( true );
GC.SuppressFinalize( this );
}
private void Dispose( bool disposing)
{
if ( ! this .bDisposed)
{
if (disposing)
{
if (appExcel != null )
{
appExcel.Quit();
// 释放COM组件,其实就是将其引用计数减1
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
}
}

GC.Collect();
}

}
3. 注意,最后一定要手动调用Dispose方法。