[英]Embarcadero C++ builder Excel Automation C++ does not quit
[英]C++ Builder and Excel Automation, where to get started?
我想用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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.