赞
踩
创建 ExApp 实例 (Create ExApp Instance)
public ExApp()
获取或创建 Excel 应用程序 (Get or Create Excel Application)
在构造函数中:
this.ComObj = (XL.Application)Marshal2.GetActiveObject("Excel.Application");
// 或
this.ComObj = new XL.Application();
工作簿操作 (Workbook Operations)
public ExWorkbook LoadWorkbook(string filePath)
public List<ExWorkbook> GetWorkbooks()
public ExWorkbook GetWorkbook(string name)
public ExWorkbook GetActiveWorkbook()
工作表操作 (Worksheet Operations)
public ExWorksheet GetWorksheet(string name)
public List<ExWorksheet> GetWorksheets()
public ExWorksheet GetActiveWorksheet()
其他操作 (Other Operations)
public void Freeze()
public void UnFreeze()
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 使用别名 XL 来简化 Microsoft.Office.Interop.Excel 的引用
using XL = Microsoft.Office.Interop.Excel;
namespace Bumblebee
{
/// <summary>
/// 封装 Excel 应用程序的类,提供对 Excel 的各种操作
/// </summary>
public class ExApp
{
#region members
// Excel 应用程序对象
public XL.Application ComObj = null;
#endregion
#region constructors
/// <summary>
/// 默认构造函数:尝试获取已运行的 Excel 实例,如果失败则创建新实例
/// </summary>
public ExApp()
{
try
{
// 尝试获取已经运行的 Excel 实例
this.ComObj = (XL.Application)Marshal2.GetActiveObject("Excel.Application");
}
catch (Exception e)
{
// 如果没有运行的实例,创建一个新的
this.ComObj = new XL.Application();
}
// 如果没有活动的工作簿,添加一个新的
if (this.ComObj.ActiveWorkbook == null) this.ComObj.Workbooks.Add();
// 确保 Excel 窗口可见
if(!this.ComObj.Visible) this.ComObj.Visible = true;
}
/// <summary>
/// 复制构造函数:从另一个 ExApp 对象创建新实例
/// </summary>
/// <param name="exApp">现有的 ExApp 对象</param>
public ExApp(ExApp exApp)
{
this.ComObj = exApp.ComObj;
}
/// <summary>
/// 参数化构造函数:使用给定的 Excel.Application 对象创建实例
/// </summary>
/// <param name="comObj">Excel.Application 对象</param>
public ExApp(XL.Application comObj)
{
this.ComObj = comObj;
}
#endregion
#region properties
// 这里可以添加属性
#endregion
#region methods
/// <summary>
/// 暂停屏幕更新,提高性能
/// </summary>
public void Freeze()
{
this.ComObj.ScreenUpdating = false;
}
/// <summary>
/// 恢复屏幕更新
/// </summary>
public void UnFreeze()
{
this.ComObj.ScreenUpdating = true;
}
#region workbooks
/// <summary>
/// 加载指定路径的工作簿
/// </summary>
/// <param name="filePath">工作簿文件路径</param>
/// <returns>加载的工作簿对象</returns>
public ExWorkbook LoadWorkbook(string filePath)
{
ExWorkbook workbook = new ExWorkbook(this.ComObj.Workbooks.Open(filePath));
return workbook;
}
/// <summary>
/// 获取所有打开的工作簿
/// </summary>
/// <returns>工作簿对象列表</returns>
public List<ExWorkbook> GetWorkbooks()
{
List<ExWorkbook> output = new List<ExWorkbook>();
foreach (XL.Workbook workbook in this.ComObj.Workbooks)
{
output.Add(new ExWorkbook(workbook));
}
return output;
}
/// <summary>
/// 根据名称获取特定工作簿
/// </summary>
/// <param name="name">工作簿名称(不包括扩展名)</param>
/// <returns>找到的工作簿对象,如果未找到则返回 null</returns>
public ExWorkbook GetWorkbook(string name)
{
ExWorkbook output = new ExWorkbook();
foreach (XL.Workbook workbook in this.ComObj.Workbooks)
{
string file = Path.ChangeExtension(workbook.Name, null);
if (file == name)
{
output.Set(workbook);
return output;
}
}
return null;
}
/// <summary>
/// 获取当前活动的工作簿
/// </summary>
/// <returns>活动工作簿对象</returns>
public ExWorkbook GetActiveWorkbook()
{
ExWorkbook output = new ExWorkbook();
if (this.ComObj.Workbooks.Count < 1)
{
output.Set(this.ComObj.Workbooks.Add());
}
else
{
output.Set(this.ComObj.ActiveWorkbook);
}
return output;
}
#endregion
#region worksheets
/// <summary>
/// 根据名称获取特定工作表
/// </summary>
/// <param name="name">工作表名称</param>
/// <returns>找到的工作表对象,如果未找到则返回 null</returns>
public ExWorksheet GetWorksheet(string name)
{
ExWorksheet output = new ExWorksheet();
foreach (XL.Worksheet worksheet in this.ComObj.Worksheets)
{
if (worksheet.Name == name)
{
output = new ExWorksheet(worksheet);
return output;
}
}
return null;
}
/// <summary>
/// 获取所有工作表
/// </summary>
/// <returns>工作表对象列表</returns>
public List<ExWorksheet> GetWorksheets()
{
List<ExWorksheet> worksheets = new List<ExWorksheet>();
foreach (XL.Worksheet sheet in this.ComObj.Worksheets)
{
worksheets.Add(new ExWorksheet(sheet));
}
return worksheets;
}
/// <summary>
/// 获取当前活动的工作表
/// </summary>
/// <returns>活动工作表对象</returns>
public ExWorksheet GetActiveWorksheet()
{
ExWorksheet output = new ExWorksheet();
if (this.ComObj.ActiveSheet == null)
{
XL.Workbook book = this.ComObj.Workbooks.Add();
XL.Worksheet sheet = book.Worksheets.Add();
output = new ExWorksheet(sheet);
}
else
{
output = new ExWorksheet(this.ComObj.ActiveSheet);
}
return output;
}
#endregion
#endregion
#region overrides
/// <summary>
/// 重写 ToString 方法,返回类的字符串表示
/// </summary>
/// <returns>类的字符串表示</returns>
public override string ToString()
{
return "Excel | App";
}
#endregion
}
}
这段代码定义了一个名为 ExApp
的类,它是对 Microsoft Excel 应用程序的封装。这个类提供了一系列方法来操作 Excel 应用程序、工作簿和工作表。下面是对代码的详细解释:
命名空间和引用:
Bumblebee
命名空间中。Microsoft.Office.Interop.Excel
命名空间,别名为 XL
。类成员:
ComObj
: 类型为 XL.Application
,表示 Excel 应用程序对象。构造函数:
ExApp
对象创建新实例。XL.Application
对象创建实例。方法:
Freeze()
和 UnFreeze()
: 控制 Excel 的屏幕更新。LoadWorkbook()
: 打开指定路径的工作簿。GetWorkbooks()
: 获取所有打开的工作簿。GetWorkbook()
: 根据名称获取特定工作簿。GetActiveWorkbook()
: 获取当前活动的工作簿。GetWorksheet()
: 根据名称获取特定工作表。GetWorksheets()
: 获取所有工作表。GetActiveWorksheet()
: 获取当前活动的工作表。重写方法:
ToString()
: 返回类的字符串表示。关键点:
学习建议:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。