永发信息网

关于操作EXECL文件的问题 关于EXECL OLE 对象的属性知多少

答案:1  悬赏:80  手机版
解决时间 2021-03-11 08:53
  • 提问者网友:嘚啵嘚啵
  • 2021-03-10 13:00
关于操作EXECL文件的问题 关于EXECL OLE 对象的属性知多少
最佳答案
  • 五星知识达人网友:污到你湿
  • 2021-03-10 14:18
1、Excel中常用的对象是:Application,Workbooks,Worksheets等。
(1)创建应用对象:如:
Variant ex;
ex=Variant::CreateObject ("Excel.Application");
或者 ex=CreateOleObject ("Excel.Application");
(2)创建工作簿对象:
Variant wb;
wb=ex.OlePropertyGet("ActiveWorkBook");
(3)创建工作表对象:
Variant sheet;
sheet=wb.OlePropertyGet("ActiveSheet");
(4)创建区域对象:
Variant range;
range=sheet.OlePropertyGet("Range","A1:A10");
2、常用的属性操作:
(1)新建EXCEL文件:
(a):新建系统模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add") //默认工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add",1) //单工作表
ex.OlePropertyGet("workbooks").OleFunction("Add",2) //图表
ex.OlePropertyGet("workbooks").OleFunction("Add",3) //宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",4) //国际通用宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",5) //与默认的相同
ex.OlePropertyGet("workbooks").OleFunction("Add",6) //工作簿且只有一个表
(b):新建自己创建的模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add","C:\\WINDOWS\\Profiles\\test2\\Application Data\\Microsoft\\Templates\\result.xlt"); // 后面写上模板的完全路径,注意“\\”
(2)打开工作簿:
ex.OlePropertyGet("workbooks").OleFunction("open","路径名.xls")
(3)保存工作簿:
wb.OleFunction("Save"); //表格保存
wb..OleFunction("SaveAs","文件名"); //表格保存为,文件路径注意用“\\”
(4)退出EXCEL:
ex.OleFunction ("Quit");
(5)设置字体:
(a):设置单元格字体
sheet.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Name","隶书");
sheet.OlePropertyGet("Cells",2,3).OlePropertyGet("Font").OlePropertySet("size",28);
(b):设置所选区域字体
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 //下划线
-----Italic: true/false //斜体
(6)单元格的合并:
(a) range1=sheet.OlePropertyGet("Range", "A1:A2"); //A1和A2单元格合并
(b) AnsiString Str="A"+IntToStr(j)+":"+"C"+IntToStr(j);
range1=sheet.OlePropertyGet("Range",Str); //可以用变量控制单元格合并
range1.OleFunction("Merge" , false);
(7)赋值语句:
(a):指定单元格赋值
sheet.OlePropertyGet("Cells",3,6).OlePropertySet("Value",str);
sheet.OlePropertyGet("Cells",j,1).OlePropertySet("Value","共查到记录:"+IntToStr(j-6));
(b):所选区域单元格赋值
range.OlePropertyGet("Cells").OlePropertySet("Value",10);
(c):所选区域行赋值
range.OlePropertyGet("Rows",1).OlePropertySet("Value",1234);
(d):工作表列赋值
sheet.OlePropertyGet("Columns",1).OlePropertySet("Value",1234);
(8)取值语句:
AnsiString abc=sheet.OlePropertyGet("Cells",120,1).OlePropertyGet("Value");
(9)区域选择:
range.OlePropertyGet("Cells").OleFunction("Select");
(10)窗口属性:
(a)显示属性
ex.OlePropertySet("Windowstate",3); //最大化显示
参数 1---------xlNormal //正常显示
2---------xlMinimized //最小化显示
3---------xlMaximized //最大化显示
(b)状态栏属性
ex.OlePropertySet ("StatusBar","您好,请您稍等。正在查询!");
ex.OlePropertySet ("StatusBar", false); //还原成默认值
(c)标题属性:
ex.OlePropertySet("Caption","查询系统");
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯