★★★★★FireGrass★★★★★

※※※※※ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞジ
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

读取excel(不需要GC.Collect();来释放进程)

Posted on 2006-08-17 13:36  火草  阅读(628)  评论(0编辑  收藏  举报

 

using System;
using System.IO;

namespace ExcelUtility
{
 
/// <summary>
 
/// 基类
 
/// </summary>

 public class ExcelBase : IDisposable
 
{
  
private Excel.Application thisApplication ;
  
private Excel.Workbooks thisWorkbooks ;
  
private Excel.Workbook thisWorkbook ;
  
protected Excel.Sheets thisSheets;
  
protected string _path ;
  
private bool _disposed ;

  
public ExcelBase(){
   
this._disposed = true ;
  }


  
public virtual void Open(string path)
  
{
   
try
   
{
    
this._path = path ;
    
this.thisApplication = new Excel.ApplicationClass();
    
this.thisWorkbooks = this.thisApplication.Workbooks;
    
this.thisWorkbook = this.thisWorkbooks.Add(path);
    
this.thisSheets = this.thisWorkbook.Worksheets ;
    
this._disposed = false
   }

   
catch(Exception ex)
   
{
    
this._releaseExcelObject();
    
throw ex ;
   }

  }


  
private void _releaseExcelObject()
  
{
   
if(this.thisSheets != null){
    System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.thisSheets);}

   
if(this.thisWorkbook != null){
    
if(File.Exists(this._path))
    
{
     
this.thisWorkbook.Close(null,_path,null);
    }

    System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.thisWorkbook);}

   
if(this.thisWorkbooks != null){
    System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.thisWorkbooks);}

   
if(this.thisWorkbook != null){
    
this.thisApplication.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.thisApplication);}

  }


  
IDisposable 成员
 }

}

 

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelUtility
{
 
/// <summary>
 
/// 继承基类
 
/// </summary>

 public class ExcelReader : ExcelBase
 
{
  Excel.Worksheet _sheet ;
  
private Excel.Range _usedRange ;
  
private Excel.Range _cells ;

  
public ExcelReader() {}

  
public string Read(int row,int column)
  
{
   Excel.Range _cell 
= null ;
   
try
   
{
    _cell 
= (Excel.Range)_cells[row,column];
    
string s =  _cell.Text.ToString();
    
return s;
   }

   
finally
   
{
    System.Runtime.InteropServices.Marshal.ReleaseComObject(_cell);
   }

  }


  
private void _getExcelObject()
  
{
   _sheet 
= (Excel.Worksheet)this.thisSheets[1] ;
   _usedRange 
= _sheet.UsedRange;
   _cells 
= _usedRange.Cells;
  }


  
private void _releaseExcelObject()
  
{
   
if(_cells != null){System.Runtime.InteropServices.Marshal.ReleaseComObject(_cells);}
   
if(_usedRange != null){System.Runtime.InteropServices.Marshal.ReleaseComObject(_usedRange);}
   
if(_sheet != null){ System.Runtime.InteropServices.Marshal.ReleaseComObject(_sheet);}
  }


  
public override void Dispose()
  
{
   
try
   
{
    
this._releaseExcelObject();}

   
finally
   
{
    
base.Dispose ();}

  }


  
public override void Open(string path)
  
{
   
try
   
{
    
base.Open (path);
    
this._getExcelObject();
   }

   
catch(Exception ex)
   
{
    
this._releaseExcelObject();
    
throw ex ;
   }

  }

 }

}