读取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 ;
}
}
}
}
- 读取excel(不需要GC.Collect();来释放进程)
- GC.Collect
- Excel释放进程
- C#释放Excel进程
- Excel释放进程
- GC.Collect()简介
- C# GC.Collect()
- 关于调用GC释放Office进程的方法
- 完全释放Excel进程的资源
- 释放excel进程的小tips
- C# 如何关闭,释放Excel进程
- hadoop streaming 慎用gc.collect()
- VC读取EXCEL后退出EXCEL进程
- Silverlight 操作Excel 中的进程资源释放问题
- 释放内存对象和杀死进程 C# Excel
- C# 释放excel 杀掉excel 而其它Excel进程则相安无事
- 【总结】终于解决了asp.net下excel导出excel.exe进程无法释放的问题!!
- C#导出Excel后关闭进程(EXCEL.EXE)释放资源的解决方案
- EXPORT EXPERIENCES(ENGLISH WORDS PERFECT!! SUCCESSFULL OPERATION!!!)
- ADO.NET开发最佳实践
- 第二周
- 职业生涯060630 career-zzf
- 怎么判断你是否是IT圈里人
- 读取excel(不需要GC.Collect();来释放进程)
- [08-01] 解决Hupigon.bwt、ZhengTu.bn、QQPass.hd等(第2版)
- [转摘]我家没"贞操"这表妹—论伪利他主义和崇高
- WEB标准开发中的一些基本用法
- 一些容易遗忘的小技巧
- 利用CDialogBar来实现类似工具栏的浮动条
- 揭开AJAX神秘面纱
- 数论算法函数库
- 拖放功能的实现