没有合适的资源?快使用搜索试试~ 我知道了~
首页C#操作Excel(Excel对象模型)_v1.0
C#操作Excel(Excel对象模型)_v1.0
5星 · 超过95%的资源 需积分: 35 391 下载量 56 浏览量
更新于2023-03-16
评论 8
收藏 1.58MB PDF 举报
Excel对象模型中文版。若要使用C#语言进行Excel操作,必须首先了解Excel的对象模型。本文用很多实例代码详细的介绍了Excel的对象模型
资源详情
资源评论
资源推荐
Excel 对象模型 (.Net Perspective)
本文主要针对在 Visual Studio 中使用 C# 开发关于 Excel 的应用程序
来源:Understanding the Excel Object Model from a .NET Developer's Perspective
Excel 对象模型的四大部分:
Application : Excel 应用程序本身
Workbook Class : 工作表 Sheet 的集合
Worksheet Class : 一个工作表 Sheet
The Range Object : 范围对象
1. Application Object
首先讨论 Application 对象
Application 对象的成员大概可以分成 5 大类
控制 Excel 的状态与显示
返回对象
执行 Action
控制文件操作(handle file manipulation)
其他
1.1 控制 Excel 的状态与显示
Property Type Description
Cursor XlMousePointer
设置/取得 鼠标指针的 Appearence
Ruby's Blog
(xlDefault,
xlIBeam,
xlNorthwestArrow
, xlWait)
EditDirectlyInCell Boolean Gets or sets the ability to edit cells directly in
place. If False, you can only edit cells in the
formula bar.
FixedDecimal Boolean If True, all numeric values use
the FixedDecimalPlacesproperty to determine
the number of decimal places;
otherwise, FixedDecimalPlaces property is
ignored (the default value is False).
FixedDecimalPlaces Long Determines the number of decimal places to be
used for numeric data if
the FixedDecimal property is True.
Interactive Boolean Gets or sets the ability of the user to interact
with Excel via the keyboard and mouse; if you
set this property to False, make absolutely sure
you set it back to True in your exception
handler. Excel won't reset it for you.
MoveAfterReturn Boolean
如果为真,表示按下回车键后移动到下一个单元
格
MoveAfterReturnDir
ection
xlDirection
(xlDown, xlToLeft,
xlToRight, xlUp)
设置按下回车键后,单元格 focus 的移动方向,
( 仅在 MoveAfterReturn 属性为真时有效)
默认移动方向:xlDown.
ScreenUpdating Boolean If True, Excel updates its screen after each
method call. To save time, and to make your
application look more professional, you can
turn off the display while your code is running.
Make sure you reset this property to True again
once you're done. Excel won't reset it for you.
SheetsInNewWorkb
ook
Long Gets or sets the number of sheets Excel
automatically places in new workbooks
Ruby's Blog
StandardFont String Gets or sets the name of the default font in
Excel; doesn't take effect until you restart Excel.
StandardFontSize Long Gets or sets the size of the default font in Excel;
doesn't take effect until you restart Excel.
StartupPath
(read-only
String Returns the complete path of the folder
containing the Excel startup add-ins.
TemplatesPath
(read-only)
String Returns the complete path of the folder
containing templates; this value represents one
of the Windows special folders.
上面列出的属性中,比较重要的是:ScreenUpdating
它的含义是:是否在任一函数执行完成后更新 Excel 的界面显示。如果你在
进行大数据量的计算,这一功能尽量关闭,因为这可以提高你的任务执行速度。
在 C#中的使用方法是:(执行完任务记得重新开启该功能)
// C#
try
{
ThisApplication.ScreenUpdating = false;
// Do your work that updates the screen.
}
finally
{
ThisApplication.ScreenUpdating = true;
}
下面再介绍几个有关属性:
Property Type Description
DisplayAlerts Boolean If True (the default value), Excel displays warning messages
while your code runs, as necessary--when deleting a sheet,
for example. Set to False to bypass warnings. Excel acts as
if you had selected the default value for each alert.
Ruby's Blog
DisplayFormulaBar Boolean If True (the default value), Excel displays the standard
formula bar for editing cells; set to False to hide the
editing bar.
DisplayFullScreen Boolean If True, Excel runs in full-screen mode (which has a
different effect from simply maximizing the Excel window);
the default value is False.
1.2 Members who return objects
Excel 库包含很多类,但是在 Visual Studio 中提供给开发人员的只有
Excel.Application, Workbook 等,那么我们怎么获得其他很多有用的类型呢?
比如,我想获得当前活动的单元格(cell),怎么办?不用担心,Application
对象提供很多属性能够返回其他很多类型的对象的引用。下面具体来看一下:
Property Type Description
ActiveCell Range Returns a reference to the currently
active cell in the active window (the
window that's on top). If there's no
active window, this property raises
an error.
ActiveChart Chart Returns a reference to the currently
active chart. An embedded chart is
only considered active when it's
selected or activated.
ActiveSheet Object Returns a reference to the active
sheet in the active workbook.
ActiveWindow Window Returns a reference to the active
window (the window that's on top);
returns Nothing if there are no
active windows.
Charts Sheets Returns a collection
Ruby's Blog
of Sheet objects (the parent for
bothChart and Worksheet objects)
containing references to each of
the charts in the active workbook.
Selection Object Returns the selected object within
the application. Might be a Range,
a Worksheet, or any other
object—also applies to
the Window class, in which case
the selection is generally
a Range object. If no object is
currently selected, returns Nothing.
Sheets Sheets Returns a collection
of Sheet objects containing
references to each of the sheets in
the active workbook.
Workbooks Workbooks Returns a collection
of Workbook objects containing
references to all the open
workbooks.
上面的属性中,Workboos 属性无疑是最常用的。通过这个属性,我们可以
打开或者创建一个 workbook,下面我们具体看一下该属性的一些行为。
1. 创建 workbook
Excel.Workbook wb = ThisApplication.Workbooks.Add(Type.Missing);
2. 关闭 workbook
ThisApplication.Workbooks.Close();
3. 打开一个存在的 workbook
Excel.Workbook wb = ThisApplication.Workbooks.Open(
"C:\\YourPath\\Yourworkbook.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Ruby's Blog
剩余34页未读,继续阅读
ruby97
- 粉丝: 618
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- ExcelVBA中的Range和Cells用法说明.pdf
- 基于单片机的电梯控制模型设计.doc
- 主成分分析和因子分析.pptx
- 共享笔记服务系统论文.doc
- 基于数据治理体系的数据中台实践分享.pptx
- 变压器的铭牌和额定值.pptx
- 计算机网络课程设计报告--用winsock设计Ping应用程序.doc
- 高电压技术课件:第03章 液体和固体介质的电气特性.pdf
- Oracle商务智能精华介绍.pptx
- 基于单片机的输液滴速控制系统设计文档.doc
- dw考试题 5套.pdf
- 学生档案管理系统详细设计说明书.doc
- 操作系统PPT课件.pptx
- 智慧路边停车管理系统方案.pptx
- 【企业内控系列】企业内部控制之人力资源管理控制(17页).doc
- 温度传感器分类与特点.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论30