C++ Builder 和 Excel Automation,从哪里开始?

标签 c++ excel automation c++builder

我想使用 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.hcomobj.hpp

关于C++ Builder 和 Excel Automation,从哪里开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010347/

相关文章:

excel - 带有 IF 的 VBA Excel 中的函数

php - 如何获取属性值类型(ss :Name) from xml element with php

java - Selenium Webdriver 和 SoapUI 有什么区别?

c++ - c++ : #define MBGL_DEFINE_ENUM(T, 值中的省略号是什么...)?

c++ - 如何在 C++ 中将外部类的模板类型用作内部类中的字段?

c++ - 无法理解符号 : * and ** with pointers

vba - 在单元格中输入公式会生成应用程序定义或对象定义的错误

c++ - 为什么不按默认构建的路径划分路径只需在 Visual Studio 中添加尾随分隔符?

automation - 如何在 Ubuntu 16 中下载 Appium Desktop

excel - 使用 Powershell 添加新列并填充工作表名称