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

来源:互联网 发布:line交友软件 编辑:程序博客网 时间:2024/06/05 14:57

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);}
  }

  #region IDisposable 成员

  public virtual void Dispose()
  {
   // TODO:  添加 ExcelImporter.Dispose 实现
   if(!this._disposed)
   {
    this._releaseExcelObject();
   }
  }

  #endregion
 }
}

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 ;
   }
  }
 }
}