excel的OLE操作方法

上传人:lisu****2020 文档编号:100970375 上传时间:2022-06-04 格式:DOC 页数:4 大小:20KB
收藏 版权申诉 举报 下载
excel的OLE操作方法_第1页
第1页 / 共4页
excel的OLE操作方法_第2页
第2页 / 共4页
excel的OLE操作方法_第3页
第3页 / 共4页
资源描述:

《excel的OLE操作方法》由会员分享,可在线阅读,更多相关《excel的OLE操作方法(4页珍藏版)》请在装配图网上搜索。

1、C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。 首先定义以下几个变量: Variant ExcelApp,Workbook1,Sheet1,Range1; 1、Excel中常用的对象是:Application,Workbooks,Worksheets等。 ★ 创建应用对象★ Variant ExcelApp; ExcelApp = Varian

2、t::CreateObject ("Excel.Application");  或者 ExcelApp = CreateOleObject ("Excel.Application"); ★ 创建工作簿对象★ Variant WorkBook1; WorkBook1 = ExcelApp.OlePropertyGet("ActiveWorkBook"); ★ 创建工作表对象★ Variant Sheet1; Sheet1 = WorkBook1.OlePropertyGet("ActiveSheet"); ★ 创建区域对象★   Variant Range; Range =

3、 Sheet1.OlePropertyGet("Range","A1:A10");   或者使用 Excel.Exec(PropertyGet("Range")<<"A1:C1").Exec(Procedure("Select")); 2、常用的属性操作: ★ 使Excel程序不可见★ ExcelApp.OlePropertySet("Visible", (Variant)false); ★ 新建EXCEL文件★ ◎ 新建系统模板的工作簿 ExcelApp.OlePropertyGet("workbooks").OleFunction("Add") //默认工作簿 Exce

4、lApp.OlePropertyGet("workbooks").OleFunction("Add", 1) //单工作表 ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", 2) //图表   ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", 3) //宏表 ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", 4) //国际通用宏表 ExcelApp.OlePropertyGet("workbooks"

5、).OleFunction("Add", 5) //与默认的相同 ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", 6) //工作簿且只有一个表 或者使用ExcelApp的Exec方法 Excel.Exec(PropertyGet("Workbooks")).Exec(Procedure("Add")); ◎ 新建自己创建的模板的工作簿  ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", "C:\\Temp\\result.xlt"); ★ 打开工作簿★ E

6、xcelApp.OlePropertyGet("workbooks").OleFunction("open", "路径名.xls")   ★ 保存工作簿★ WorkBook1.OleFunction("Save"); //保存工作簿 WorkBook1.OleFunction("SaveAs", "文件名");//工作簿保存为,路径注意用"\\" ★ 退出EXCEL★ ExcelApp.OleFunction("Quit"); ExcelApp = Unassigned; 或者 ExcelApp.Exec(Procedure("Quit")); ★ 操作工作表★ ◎ 选择

7、选择工作表中第一个工作表 Workbook1.OlePropertyGet("Sheets", 1).OleProcedure("Select"); Sheet1 = Workbook1.OlePropertyGet("ActiveSheet"); ◎ 重命名工作表 Sheet1.OlePropertyGet("Name", "Sheet的新名字");   ◎当前工作簿中的工作表总数   int nSheetCount=Workbook1.OlePropertyGet("Sheets").OlePropertyGet("Count");   ★ 操作行和列★ ◎ 获取当前工作

8、表中有多少行和多少列: Sheet1.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count"); //列数 Sheet1.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count"); //行数 ◎ 设置列宽 ExcelApp.OlePropertyGet("Columns", 1).OlePropertySet("ColumnWidth", 22); 或者 Range = ExcelApp.Ole

9、PropertyGet("Cells", 1, 3); Range.OlePropertySet("ColumnWidth", 22);   ◎ 设置行高 ExcelApp.OlePropertyGet("Rows", 2).OlePropertySet("RowHeight", 25); 或者 Range = ExcelApp.OlePropertyGet("Cells", 2, 1);  Range.OlePropertySet("RowHeight", 25);   ◎ 在工作表最前面插入一行 Sheet1.OlePropertyGet("Rows", 1).Ole

10、Procedure("Insert");  ◎ 删除一行 ExcelApp.OlePropertyGet("Rows", 2).OleProcedure("Delete"); //将第2行删除★ 操作单元格★ ◎ 设置单元格字体 Sheet1.OlePropertyGet("Cells", 1, 1).OlePropertyGet("Font").OlePropertySet("Name", "隶书"); //字体 Sheet1.OlePropertyGet("Cells", 2, 3).OlePropertyGet("Font").OlePropertySet("size", 2

11、8); //大小 ◎ 设置所选区域字体 Range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Size", 28); Range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Color", RGB(0, 0, 255)); 其中参数的设置: Font Name : "隶书" //字体名称 Size : 12 //字体大小 Color : RGB(*,*,*) //颜色 Underline : true/false

12、//下划线 Italic: true/false //斜体 ◎ 设置单元格格式为小数百分比 Sheet1.OlePropertyGet("Cells", 1, 1).OlePropertySet("NumberFormatLocal", "0.00%"); ◎ 设定单元格的垂直对齐方式 Range = ExcelApp.OlePropertyGet("Cells", 3, 4); // 1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐 Range.OlePropertySet("VerticalAlignment", 2);   ◎ 设定单元格的文本为自动换行 R

13、ange = ExcelApp.OlePropertyGet("Cells", 3, 4); Range.OlePropertySet("WrapText", true); ★ 单元格的合并★ ◎ Range = Sheet1.OlePropertyGet("Range", "A1:A2"); //A1和A2单元格合并 String strRange = "A" + IntToStr(j) + ":" + "C" + IntToStr(j); //比如:A1:C5 Range1=Sheet1.OlePropertyGet("Range", strRange.c_str()); //可

14、以用变量控制单元格合并 Range1.OleFunction("Merge", false); ★ 读写单元格★ ◎ 指定单元格赋值 String strValue = "abcdefg"; Sheet1.OlePropertyGet("Cells", 3, 6).OlePropertySet("Value", strValue.c_str()); Sheet1.OlePropertyGet("Cells", j, 1).OlePropertySet("Value", "总记录:" + String(j-6)); 或者使用 Excel.Exec(PropertyGet("Cel

15、ls")<<1<<3).Exec(PropertySet("Value")<<15); ◎ 所选区域单元格赋值 Range.OlePropertyGet("Cells").OlePropertySet("Value", 10);   ◎ 所选区域行赋值 Range.OlePropertyGet("Rows",1).OlePropertySet("Value", 1234);   ◎ 工作表列赋值 Sheet1.OlePropertyGet("Columns",1).OlePropertySet("Value", 1234);  ◎ 读取取值语句: String strVa

16、lue = Sheet1.OlePropertyGet("Cells", 3, 5).OlePropertyGet("Value"); ★ 窗口属性★ ◎ 显示属性 ExcelApp.OlePropertySet("Windowstate", 3); //最大化显示 1---------xlNormal //正常显示 2---------xlMinimized //最小化显示 3---------xlMaximized //最大化显示 ◎ 状态栏属性 ExcelApp.OlePropertySet("StatusBar", "您好,请您稍等。正在查询!"); ExcelApp.OlePropertySet("StatusBar", false); //还原成默认值 ◎ 标题属性: ExcelApp.OlePropertySet("Caption", "查询系统");

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

相关资源

更多
正为您匹配相似的精品文档
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!