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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`gc.collect()`是Python中的一个函数,用于显式地触发垃圾回收(Garbage Collection)。垃圾回收是一种自动内存管理机制,用于回收不再被程序使用的内存空间,以便重新分配给其他对象。 在Python中,垃圾回收是通过引用计数和循环垃圾收集(Cycle Garbage Collection)来实现的。引用计数是一种简单且高效的垃圾回收机制,它会跟踪每个对象被引用的次数,当引用计数为0时,对象就可以被回收。 然而,在某些情况下,循环引用(两个或多个对象之间相互引用)可能导致引用计数无法正常工作。这时,需要使用循环垃圾收集来检测和回收这些循环引用的对象。`gc.collect()`函数可以手动触发循环垃圾收集的过程,以便及时清理不再被使用的对象。 使用`gc.collect()`的方式如下: ```python import gc # 在适当的时机调用collect函数 gc.collect() ``` 需要注意的是,一般情况下不需要显式地调用`gc.collect()`函数,因为Python会自动进行垃圾回收。只有在特殊情况下,如内存占用过高或存在大量循环引用时,才需要考虑手动触发垃圾回收。 此外,`gc.collect()`函数会根据当前的垃圾回收策略进行回收。默认情况下,Python使用的是自动垃圾回收策略,通过调整`gc`模块的相关参数,可以对垃圾回收进行一些配置,如设置阈值、调整引用计数等。但一般情况下,使用默认的垃圾回收策略即可满足需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值