我想使用 C++ builder 2009 动态创建和填充 excel 电子表格,但我不完全确定如何去做。在网上搜索后,我将范围缩小到使用 OLE 自动化。此外,我正在寻找可以帮助我入门的文档或编程教程。有没有一个简单的编程教程,也彻底解释了 OLE 自动化的概念?
最佳答案
打开 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.h
和 comobj.hpp
。
关于C++ Builder 和 Excel Automation,从哪里开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010347/