Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 12-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Exportar datos a excel desde embarcadero c++

Hola, estoy intentando exportar datos de una consulta a una hoja excel, pero me da error, este es el código que utilizo:
Código PHP:
void __fastcall exportarAExcel(TADOQuery *q,String hoja,String fichero)
{
        
Variant _excel;
        
Variant _libro;
        
Variant _hoja;
        
Variant _celda;
        
Variant aux NULL;
        
String txt "";
        
int fila 1;//me almacena en qué fila del Excel estoy escribiendo
        
int columna 1;//me almacena en qué columna del Excel estoy escribiendo

        
try{
                
_excel CreateOleObject("Excel.Application");
                
//doy valor al libro a partir de la aplicación: su libro
                
_libro _excel.OlePropertyGet("Workbooks");
                
//le digo lo que tiene que hacer: añadir hojas
                
_libro.OleProcedure("Add");
                
//asigno a la hoja el primer item del libro
                
_hoja _libro.OlePropertyGet("Item",1);
                
//
                
_libro _hoja.OlePropertyGet("Worksheets");
                
//Libro=Hoja.OlePropertyGet("Worksheets");
                
_libro.OlePropertyGet("Item",1).OlePropertySet("Name",hoja);

                
//RECORRO LA CONSULTA INSERTANDO LAS CELDAS

                
_hoja _libro.OlePropertyGet("Item",1);
                
//primero relleno las cabeceras: LA PRIMERA FILA (letra en blanco negrita y fondo negro)
                
for(columna 1columna <= q->Fields->Count;columna++){
                        
txt q->Fields->Fields[columna-1]->FieldName;
                        
_hoja.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertySet("Value",txt );
                        
_hoja.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Font").OlePropertySet("Color",clBlack);
                        
_hoja.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Font").OlePropertySet("Bold",true);
                        
_hoja.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Interior").OlePropertySet("ColorIndex",clBlack);
                }
                
fila 2;

                while( !
q->Eof ){
                        for(
columna 1columna <= q->Fields->Count;columna++){
                                
txt q->Fields->Fields[columna-1]->AsString;
                                
_hoja.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertySet("Value",txt );
                                
//vuelvo a empezar desde la primera columna: COLUMNA = 1
                        
}
                        
q->Next();//avanzo al siguiente registro: cambio de línea
                        
fila++;
                }

                
_excel.OlePropertySet("DisplayAlerts",false); //suppress the warning dialog on closing the server
                //_excel.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs",fichero);
                //la pongo en visible
                
_excel.OlePropertySet("Visible",true);
                
//_excel.OleProcedure("Quit");
                //_excel = Unassigned;
        
}
        catch(
Exception *e){
                
_excel.OlePropertySet("DisplayAlerts",false); //suppress the warning dialog on closing the server
                
_excel.OleProcedure("Quit");
                
_excel Unassigned;
        }

¿Me podría ayudar?
Gracias!!

Última edición por ecfisa fecha: 12-11-2020 a las 14:29:04. Razón: Agregar etiquetas al código
Responder Con Cita
  #2  
Antiguo 12-11-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.040
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Hola, bienvenido a clubdelphi, por favor, revisa nuestra guía de estilo, gracias
Responder Con Cita
  #3  
Antiguo 12-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Hola, ya me la he leído. El código que adjunto es el que teníamos en el Borland 5 pero al migrar a Embarcadero no funciona. No he encontrado en el foro otra manera de hacerlo en c++. Me da error de tipo de variable en la fila 23.
Gracias!
Responder Con Cita
  #4  
Antiguo 12-11-2020
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola tekito.

Cuando agregues código a tu mensaje usa las etiquetas:

[php]
...codigo...
[/php]

para una mejor visualización, gracias.

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #5  
Antiguo 12-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Ok! Gracias!
Responder Con Cita
  #6  
Antiguo 12-11-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.040
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Cita:
Empezado por tekito Ver Mensaje
Hola, ya me la he leído. El código que adjunto es el que teníamos en el Borland 5 pero al migrar a Embarcadero no funciona. No he encontrado en el foro otra manera de hacerlo en c++. Me da error de tipo de variable en la fila 23.
Gracias!
Y no podemos saber cuál es la línea 23 porque puede haber más código antes de lo que has publicado.
Responder Con Cita
  #7  
Antiguo 13-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Hola, la fila no ha cambiado, sigue siendo la 23, me da error en el tipo de variable.
Aquí:
_libro.OlePropertyGet("Item",1).OlePropertySet("Name",hoja);
Gracias.
Responder Con Cita
  #8  
Antiguo 13-11-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.040
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Cita:
Empezado por tekito Ver Mensaje
Hola, la fila no ha cambiado, sigue siendo la 23, me da error en el tipo de variable.
Aquí:
_libro.OlePropertyGet("Item",1).OlePropertySet("Name",hoja);
Gracias.

No sé qué tipo de variable espera, ni sabemos de qué tipo es tu variable "hoja", por ejemplo.


Y recuerda usar las etiquetas de código, como te han comentado antes, gracias.
Responder Con Cita
  #9  
Antiguo 13-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Tienes razón, hoja es de tipo texto:
Código PHP:
  Variant _libro;
_libro.OlePropertyGet("Item",1).OlePropertySet("Name","habitantes"); 
Responder Con Cita
  #10  
Antiguo 13-11-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.040
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Pues no sé, pero "propery set" parece que es para proporcionarle un valor a alguna propiedad, puede que espere un entero, por ejemplo número de página.
Mira esto.
Y "habitantes" no creo que sea una propiedad.
Responder Con Cita
  #11  
Antiguo 13-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Tienes razón espera un entero, si lo cambio por un entero funciona.
¿Pero la propiedad no sería "Name"? ¿No se haría así para ponerle nombre a la hoja de excel?
Código PHP:
_libro.OlePropertyGet("Item",1).OlePropertySet("Name","habitantes"); 
"habitantes" sería el nombre de la hoja de excel.
Muchas gracias!
Responder Con Cita
  #12  
Antiguo 18-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
¿me echáis una mano, porfa, que estoy atascado?

Saludos.
Responder Con Cita
  #13  
Antiguo 18-11-2020
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is offline
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.040
Poder: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Mira este ejemplo.
Responder Con Cita
  #14  
Antiguo 18-11-2020
tekito tekito is offline
Miembro
 
Registrado: nov 2020
Posts: 10
Poder: 0
tekito Va por buen camino
Hola, ya lo conseguí, pongo el código:
Código PHP:
void __fastcall exportarAExcel(TADOQuery *q,WideString hoja,WideString fichero)
{
        
Variant _excel;
        
Variant _libro;
        
Variant _hoja;
        
Variant _celda;
        
Variant excel_sheet;
        
Variant aux NULL;
        
WideString txt "";
        
int fila 1;//me almacena en qué fila del Excel estoy escribiendo
        
int columna 1;//me almacena en qué columna del Excel estoy escribiendo

        
try{
                
_excel CreateOleObject("Excel.Application");
                
//doy valor al libro a partir de la aplicación: su libro
                
_libro _excel.OlePropertyGet("Workbooks");
                
//le digo lo que tiene que hacer: añadir hojas
                
_libro.OleProcedure("Add");
                
//asigno a la hoja el primer item del libro
                
_hoja _libro.OlePropertyGet("Item",1);
                
//
                
_libro _hoja.OlePropertyGet("Worksheets");
                
//Libro=Hoja.OlePropertyGet("Worksheets");
                
_libro _libro.OlePropertyGet("Item"1);
                
_libro.OlePropertySet("Name",hoja);

                
//RECORRO LA CONSULTA INSERTANDO LAS CELDAS

                //////_hoja = _libro.OlePropertyGet("Item",1);
                //primero relleno las cabeceras: LA PRIMERA FILA (letra en blanco negrita y fondo negro)
                
for(columna 1columna <= q->Fields->Count;columna++){
                        
txt q->Fields->Fields[columna-1]->FieldName;
                        
_libro.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertySet("Value",txt);
                        
_libro.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Font").OlePropertySet("Color",clBlack);
                        
_libro.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Font").OlePropertySet("Bold",true);
                        
_libro.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertyGet("Interior").OlePropertySet("ColorIndex",clBlack);
                }
                
fila 2;

                while( !
q->Eof ){
                        for(
columna 1columna <= q->Fields->Count;columna++){
                                
txt q->Fields->Fields[columna-1]->AsString;
                                
_libro.OlePropertyGet("Cells").OlePropertyGet("Item",fila,columna).OlePropertySet("Value",txt);
                                
//vuelvo a empezar desde la primera columna: COLUMNA = 1
                        
}
                        
q->Next();//avanzo al siguiente registro: cambio de línea
                        
fila++;
                }

                
_excel.OlePropertySet("DisplayAlerts",false); //suppress the warning dialog on closing the server
                //_excel.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs",fichero);
                //la pongo en visible
                
_excel.OlePropertySet("Visible",true);
                
//_excel.OleProcedure("Quit");
                //_excel = Unassigned();
        
}
        catch(
Exception *e){
                
_excel.OlePropertySet("DisplayAlerts",false); //suppress the warning dialog on closing the server
                
_excel.OleProcedure("Quit");
                
_excel Unassigned();
        }

Gracias!
Responder Con Cita
  #15  
Antiguo 18-11-2020
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola tekito, gracias por compartir la solución.

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Exportar a Excel desde builder 6.0 damian2000 C++ Builder 9 22-06-2016 18:18:56
exportar datos desde excel a mysql voldemmor Varios 7 02-04-2009 15:39:03
exportar a excel desde php halizia PHP 22 29-11-2006 09:20:37
Exportar desde BDE a Excel tamara Servers 1 03-10-2003 23:05:02
Exportar desde Tabla BDE a Excel tamara Conexión con bases de datos 1 03-10-2003 18:20:03


La franja horaria es GMT +2. Ahora son las 00:41:49.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi