之前没玩过C#的类的封装,虽然我经常使用AccessHelper,但是这个是别人封装好拿给我的。
引用组件
微软提供的.net中excel组件
刚刚弄了好久C#对EXCEL简单的操作的类的封装。终于搞定。源码如下,注释里面写的很清楚啦。
-------------------------源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
using Microsoft.Office.Interop.Excel;
class ExcelHelper
{
#region 属性
///---属性初始化
public static Excel.ApplicationClass xlsx = null;
public static _Workbook workBook = null;
public static _Worksheet workSheet = null;
#endregion
#region 打开一个已经存在的EXCEL文件
///---参数是已经存在的EXCEL的文件路径(含文件格式),当前ACTIVEsheet的 index
///---若路径合理,该该函数将执行赋值操作
///---成功返回 TRUE, 失败返回 FALSE
public static bool InitExcel(string filePathStr, int sheetIndex, ref _Worksheet sheet)
{
try
{
///----
if (null != xlsx)
{
CloseExcelApplication();
}
///----检查路径是否合理
if (string.IsNullOrEmpty(filePathStr))
{
throw new Exception("文件路径不能为空,请重新传值");
}
///---判断文件是否存在
if (!File.Exists(filePathStr))
{
throw new Exception(filePathStr + ", 文件不存在");
}
else
{
///---接下来就是赋值。
///---实例化对象
xlsx = new E