【问题标题】:C++ Builder and Excel Automation, where to get started?C++ Builder 和 Excel 自动化,从哪里开始?
【发布时间】:2010-06-09 22:07:23
【问题描述】:

我想使用 C++ builder 2009 动态创建和填充 Excel 电子表格,但我不完全确定如何去做。在网上搜索,我将其范围缩小到使用 OLE 自动化。此外,我正在寻找可以帮助我入门的文档或编程教程。有没有一个简单的编程教程也能彻底解释 OLE 自动化的概念?

【问题讨论】:

    标签: c++ excel automation c++builder


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      相关资源
      最近更新 更多