C ++ Builder and Excel Automation, where to start?

I want to dynamically create and populate an Excel spreadsheet using C ++ builder 2009, but I'm not quite sure how to do this. Searching the Internet I narrowed it down to using OLE Automation. I am also looking for a document or programming tutorial that can help me get started. Is there a simple programming tutorial that also fully explains OLE automation concepts?

+3
source share
1 answer

To open an Excel spreadsheet:

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);

To get the current cell pointer:

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

To add values ​​to 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)
    );

Hope this helps you, you probably have to turn on vcl.hand comobj.hpp.

+1
source

Source: https://habr.com/ru/post/1749300/


All Articles