C++ Builder和Excel自动化,如何入门?

3

我希望能够使用C++ Builder 2009动态创建和填充Excel电子表格,但我不确定如何操作。在网上搜索后,我发现可以使用OLE自动化技术来实现。此外,我正在寻找一份文档或编程教程,可以帮助我入门学习。是否有一份简单易懂的编程教程,同时也能深入讲解OLE自动化的概念呢?

1个回答

1
打开Excel电子表格:
Variant excelApp = Unassigned;

//EOleSysError is thrown if GetActiveObject does not succeed. i.e
//if Excel is not currently open.
try
{
    excelApp = Variant::GetActiveObject("Excel.Application");
}
catch(EOleSysError& e)
{
    excelApp = Variant::CreateObject("Excel.Application"); //open excel
}

excelApp.OlePropertySet("ScreenUpdating", true);

获取当前单元格指针:

Variant excelCell = excelSheet.OlePropertyGet("Cells"); 

向 Excel 添加值:

// create a vararray of 5 elements starting at 0
int bounds[2] = {0, 4};
Variant variantValues = VarArrayCreate(bounds, 1, varVariant);

variantValues.PutElement(5, 0);
variantValues.PutElement(5, 1);
variantValues.PutElement(5, 2);
variantValues.PutElement(5, 3);
variantValues.PutElement(5, 4);

Variant cellRange = excelCell.OlePropertyGet(
    "Range", 
    excelCell.OlePropertyGet("Item", rowindex, columnindex),  // start cell  
    excelCell.OlePropertyGet("Item", rowindex2, columnindex2) // finishing cell   
    );

// place array into excel
cellRange.OlePropertySet(
    "Value", 
    excelApp.OleFunction("Transpose", variantValues)
    );

希望这能帮助你入门,你可能需要包含 vcl.hcomobj.hpp


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接