会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
★★★★★
FireGrass
★★★★★
⊙
※※※※※
⊙
ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞジ
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
读取excel(不需要GC.Collect();来释放进程)
Posted on
2006-08-17 13:36
火草
阅读(
628
) 评论(
0
)
编辑
收藏
举报
读取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);}
}
IDisposable 成员
#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 ;
}
}
}
}
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 火草
Powered by .NET 8.0 on Kubernetes