您的位置:首页 > 其它

bcb中用数组给Excel的区域赋值

2008-10-23 22:21 471 查看
今天导出Excel文件时,由于数据量比较大,一个一个单元格的写,速度比较慢慢。我在自己的机器上测试了一下,如果数据量为5万多时,需要的时间大概要8分钟左右,速度实在太慢了。我想能不能给Excel的区域赋值呢,这样的速度可能会快一点。于是自己上网查了查资料,但是这部分的资料大部分是Vb的,BCB关于区域赋值的资料几乎没有。于是自己在机器上试试一两个小时,终于给试出来了,不敢私藏,拿出来给大家看看,看看有没有更好的办法。
代码如下:

Variant ExcelApp, Sheet;

try

{

ExcelApp=Variant::CreateObject( "Excel.Application");

}

catch(...)

{

MessageBox(Handle, "无法启动Excel! ", "信息提示 ",MB_ICONSTOP|MB_OK);

return;

}

ExcelApp.OlePropertySet( "Visible",false);

ExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1);

Sheet = ExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);

String str[4];

str[0] = "11";

str[1] = "2";

str[2] = "33";

str[3] = "44";

int Bounds[4] = {0,1,0,3}; //二维数组 相当于 Datas[1][4]

Variant Datas= VarArrayCreate(Bounds, 3, varVariant);

for(int i=0;i<1;i++)

{

for(int j=0;j<4;j++)

{

Datas.PutElement(str[j],i,j);

}

}

Sheet.OlePropertyGet("range","A1:D1").OlePropertySet("Value", Datas);

ExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs", "c://11.xls");

ExcelApp.OleFunction("Quit");

Sheet = Unassigned;

ExcelApp = Unassigned;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: