📄 bcjq093.txt
字号:
C++ Builder开发者:程序员之家
var how_many_ads = 3;
var now = new Date()
var sec = now.getSeconds()
var ad = sec % how_many_ads;
ad +=1;
首页
| 控件天堂 | 控件使用
| 编程技巧
| 源代码 | 编程工具 |
系统补丁 | 电子书籍 | 技术论坛
| 相关链接
if (ad==1) {document.write(''+'');}
if (ad==2) {document.write(''+'');}
if (ad==3) {document.write(''+'');}
var marqueecontents=''+scroll_text+''
if (document.all)
document.write(''+marqueecontents+'')
function regenerate(){
window.location.reload()
}
function regenerate2(){
if (document.layers){
setTimeout("window.onresize=regenerate",450)
intializemarquee()
}
}
function intializemarquee(){
document.cmarquee01.document.cmarquee02.document.write(marqueecontents)
document.cmarquee01.document.cmarquee02.document.close()
thelength=document.cmarquee01.document.cmarquee02.document.height
scrollit()
}
function scrollit(){
if (document.cmarquee01.document.cmarquee02.top>=thelength*(-1)){
document.cmarquee01.document.cmarquee02.top-=speed
setTimeout("scrollit()",100)
}
else{
document.cmarquee01.document.cmarquee02.top=marqueeheight
scrollit()
}
}
window.onload=regenerate2
当前位置
:
编程技巧
用C++Builder操纵Excel
Microsoft
Excel作为一个功能强大的电子表格处理软件,已经得到了广泛的应用。在数据库应用软件开发过程中,如果能实现数据库与Excel文件的数据交换,那么一方面能增加数据库数据的来源,另一方面也能方便用户对数据库数据的进一步处理。
C++ Builder是一个可视化快速应用程序开发工具,它提供的OLE
Automation机制使得开发人员在应用程序中就能调用Excel,实现数据交换。
在C++ Builder中调用Excel,首先要为Excel建立一个OLE对象,然后通过设定该对象的属性和调用该对象的方法来操纵Excel。C++
Builder通过CreateOleObject()来建立OLE对象;用OlePropertySet(propname,value)来设置OLE对象的属性;用OlePropertyGet(propname)获得OLE对象的属性;通过OleFunction(oleFuncName,[val,...])和OleProcedure(oleProcName,[val,...])来调用OLE对象的方法。
C++ Builder中使用OLE自动化操纵Excel,必须掌握Excel的自动化对象及VBA关于Excel的对象方法和属性,这些都在Microsoft
Office(完全安装)的VBAXL8.HLP帮助文件中可以找到,在此就不再赘述。下面以将数据库中的数据转到Excel工作薄中为例,说明C++
Builder操纵Excel的具体方法。
首先新建一个表单Form1,保存单元文件Unit1.cpp,保存工程文件Project1.bpr。然后在表单中加入数据访问控件TTable,将Name属性设为Table1,DatabaseName属性设为BCDEMOS,TableName属性设为Country.db。在表单中加入一个按钮控件TButton,将其Name属性设为Button1,Caption属性设为
“转换为Excel文件”。双击Button1,在Button1Click()函数中加入如下代码:
Variant ex,newxls;
int i,j=1;
try
{
ex=CreateOleObject(″Excel.Application″); //启动Excel
}
catch(...)
{
ShowMessage(″无法启动Excel″);
}
ex.OlePropertySet(″Visible″,(Variant)true);
//使Excel启动后可见
newxls=(ex.OleFunction(″Workbooks″)).OleFunction(″Add″);//新建一个工作薄
Table1-〉Active=true;
//打开数据库
Table1-〉First();
for(i=0;i〈Table1-〉FieldCount;i++)
//将字段名写到工作薄的第一行
{
(ex.OleFunction(″Cells″)).OlePropertySet(″Item″,(Variant)1,(Variant)(i+1),(Variant)Table1-〉Fields[i]-〉FieldName);
}
while(!Table1-〉Eof)
//将数据库中的记录依次写到工作薄中
{
j=j+1;
for(i=0;i〈Table1-〉FieldCount;i++)
{
(ex.OleFunction(″Cells″)).OlePropertySet(″Item″,(Variant)j,(Variant)(i+1),
(Variant)Table1-〉Fields[i]-〉AsString);
}
Table1-〉Next();
}
newxls.OleFunction(″SaveAs″,(Variant)filename);
//保存工作薄,filename是工作薄的全文件名
ex.OleFunction(″Quit″);
//退出Excel,释放OLE对象
注意,要使用OLE自动化对象,在编译之前还必须在Unit1.cpp文件前面加上#include
″ComObj.hpp″。
运行程序就可以实现将数据库中的数据转换到Excel工作薄中。同样的道理,使用OlePropertyGet()函数,也可以将Excel工作薄中的数据读到数据库中去。
以上代码在Windows 98操作系统、C++
Builder 3.0下调试运行通过。
摘自《赛迪网》 赵天平/文
if (ad==1) {document.write(''+'');}
if (ad==2) {document.write(''+'');}
if (ad==3) {document.write(''+'');}
C++ Builder开发者®
2000年06月01日 站长:唐朝