excel的OLE操作方法



《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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专题党课讲稿:以高质量党建保障国有企业高质量发展
- 廉政党课讲稿材料:坚决打好反腐败斗争攻坚战持久战总体战涵养风清气正的政治生态
- 在新录用选调生公务员座谈会上和基层单位调研座谈会上的发言材料
- 总工会关于2025年维护劳动领域政治安全的工作汇报材料
- 基层党建工作交流研讨会上的讲话发言材料
- 粮食和物资储备学习教育工作部署会上的讲话发言材料
- 市工业园区、市直机关单位、市纪委监委2025年工作计划
- 检察院政治部关于2025年工作计划
- 办公室主任2025年现实表现材料
- 2025年~村农村保洁员规范管理工作方案
- 在深入贯彻中央8项规定精神学习教育工作部署会议上的讲话发言材料4篇
- 开展深入贯彻规定精神学习教育动员部署会上的讲话发言材料3篇
- 在司法党组中心学习组学习会上的发言材料
- 国企党委关于推动基层党建与生产经营深度融合工作情况的报告材料
- 副书记在2025年工作务虚会上的发言材料2篇