Excel y C++ Builder

viferpe
20 de Julio del 2003
Necesito abrir un fichero existente en Excel para leer algunas de sus columnas e insertarlas en la ventana de mi "cutre-aplicaci贸n" en C++ Builder.
¿Puede alguien contarme c贸mo hacerlo?
Gracias.

victor ya?
20 de Julio del 2003
Hay que a帽adir #include <Comobj.hpp> para BCB3, 4 y 5 o bien #include <oleauto.hpp> para BCB1

//-------- Abre EXCEL
Variant xlApp;
xlApp = CreateOleObject("Excel.Application");

//-------- hace EXCEL visible
xlApp.Exec(PropertySet("Visible") << true);

//-------- crea acceso al objeto 'libro'/workbook
Variant xlBooks = xlApp.Exec(PropertyGet("Workbooks"));

//-------- Crea un fichero nuevo
xlBooks.Exec(Procedure("Add"));
// ... o abre uno existente
// xlBooks.Exec(Procedure("Open") << "C:\directorio\fichero.xls");

//-------- coge el primer libro de la aplicacion
Variant xlBook = xlBooks.Exec(PropertyGet("Item") << 1);

//-------- crea acceso al objeto 'hoja'/worksheets
Variant xlSheets = xlBook.Exec(PropertyGet("Worksheets"));

//-------- coge la primera hoja del libro
Variant xlSheet = xlSheets.Exec(PropertyGet("Item") << 1);

//-------- Trabajando con celdas...
Variant VRange;
//-------- Escribe texto en las primeras 3x3 celdas
VRange = xlSheet.Exec(PropertyGet("Range") << "A1");
VRange.Exec(PropertySet("Value") << "CeldaA1");
VRange = xlSheet.Exec(PropertyGet("Range") << "A2");
VRange.Exec(PropertySet("Value") << "CeldaA2");
VRange = xlSheet.Exec(PropertyGet("Range") << "A3");
VRange.Exec(PropertySet("Value") << "CeldaA3");
//------
VRange = xlSheet.Exec(PropertyGet("Range") << "B1");
VRange.Exec(PropertySet("Value") << "CeldaB1");
VRange = xlSheet.Exec(PropertyGet("Range") << "B2");
VRange.Exec(PropertySet("Value") << "CeldaB2");
VRange = xlSheet.Exec(PropertyGet("Range") << "B3");
VRange.Exec(PropertySet("Value") << "CeldaB3");
//------
VRange = xlSheet.Exec(PropertyGet("Range") << "C1");
VRange.Exec(PropertySet("Value") << "CeldaC1");
VRange = xlSheet.Exec(PropertyGet("Range") << "C2");
VRange.Exec(PropertySet("Value") << "CeldaC2");
VRange = xlSheet.Exec(PropertyGet("Range") << "C3");
VRange.Exec(PropertySet("Value") << "CeldaC3");

//-------- Borra las primeras 2 x 2 celdas
VRange = xlSheet.Exec(PropertyGet("Range") << "A1:B2");
VRange.Exec(Function("ClearContents"));

//-------- Guarda el libro actual
xlBook.Exec(Procedure("SaveAs") << "c:\fichero.xls");

//-------- Cierra excel
xlApp.Exec(PropertySet("Visible") << false);
xlApp.Exec(Procedure("Quit"));
xlApp.Clear();

//encontre esto por ahi, espero te sirva ;-)