 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
rockman Ens

Registrado: 13 Mar 2004Mensajes: 44
|
Publicado: Jue 25 Mar, 2004 6:01 pm Asunto: Exportar datos de un DBGRID a un excel |
|
|
Hola,
Necesito un grid que me de la posibilidad de copiar la información, para luego pegarlo en un fichero EXCEL.
He estado viendo componentes, pero todos son shareware y dado que se trata de una aplicación sin animo de lucro, no puedo permitirmelo.
¿Conoceis de algún componente que me permita realizar esta operacion?
O en su defecto, ¿sabeis la forma de desarrollar esta operación?
Muchas gracias |
|
| Volver arriba |
|
 |
Touchsoftwares Master


Registrado: 16 Dic 2002Mensajes: 869Ubicación: MEXICO Playa del Carmen
|
Publicado: Vie 26 Mar, 2004 11:22 pm Asunto: |
|
|
con la ayuda de este foro modificamos un ejemplo y quedo asi
| Código: |
//------------------------------------------------------------
void ExportarExcel(TDataSet *qry)
{
Variant vExcel, vHoja, vRango;
vExcel=CreateOleObject("Excel.Application");
vExcel.OlePropertySet("Visible",true);
vExcel.OlePropertyGet("Workbooks").OleProcedure("Add");
vHoja=vExcel.OlePropertyGet("ActiveSheet");
int row = 1;
String COLUMNA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//para los nombres de columna
//cabeceras
for (int i = 0; i < qry->FieldDefs->Count; i++)
{
TFieldDef *fd = qry->FieldDefs->Items[i];
if(i<=25)
vExcel.OlePropertyGet("Range",COLUMNA.SubString(i+1,1)+IntToStr(row)).OleProcedure("Select");
if(i>25 && i <=51)
vExcel.OlePropertyGet("Range","A"+COLUMNA.SubString(i+1-26,1)+IntToStr(row)).OleProcedure("Select");
if(i>51 && i <=77)
vExcel.OlePropertyGet("Range","B"+COLUMNA.SubString(i+1-26,1)+IntToStr(row)).OleProcedure("Select");
if(i>77 && i <=103)
vExcel.OlePropertyGet("Range","C"+COLUMNA.SubString(i+1-78,1)+IntToStr(row)).OleProcedure("Select");
vExcel.OlePropertyGet("ActiveCell").
OlePropertySet("Value",fd->Name);
}
row++;
row++;
qry->First();
while(!qry->Eof)
{
for(int col=0;col<qry->FieldCount;col++)
{
if(col<=25)
vExcel.OlePropertyGet("Range",COLUMNA.SubString(col+1,1)+IntToStr(row)).OleProcedure("Select");
if(col>25 && col <=51)//if(col>26 <=52)
vExcel.OlePropertyGet("Range","A"+COLUMNA.SubString(col+1-26,1)+IntToStr(row)).OleProcedure("Select");
if(col>51 && col <=77)
vExcel.OlePropertyGet("Range","B"+COLUMNA.SubString(col+1-52,1)+IntToStr(row)).OleProcedure("Select");
if(col>77 && col <=103)
vExcel.OlePropertyGet("Range","C"+COLUMNA.SubString(col+1-78,1)+IntToStr(row)).OleProcedure("Select");
vExcel.OlePropertyGet("ActiveCell").
OlePropertySet("Value",qry->Fields->Fields[col]->AsString);
}
qry->Next();
row++;
}
/* for (int i=1;i<qry->FieldDef100;i++) {
char* nombreCelda;
AnsiString tmpCelda;
tmpCelda="A"+IntToStr(i);
nombreCelda=tmpCelda.c_str();
vRango=vHoja.OlePropertyGet("Range",nombreCelda);
vRango.OlePropertySet("Value",1);
}*/
vExcel=vHoja=Unassigned;
}
|
Si les funciona comentelo y si l omejoran ponga la mejora en el foto..compartir conocimientos es la mejor forma de desarrollarse
saludos _________________ Ing. Gabriel Góngora Navarrete
TouchSoftwares Inc
http://www.touchsoftwares.com |
|
| Volver arriba |
|
 |
RexL7 Cdt

Registrado: 09 Jul 2003Mensajes: 5Ubicación: México
|
Publicado: Vie 23 Abr, 2004 5:37 pm Asunto: |
|
|
Hola he probado la función tratando de que que se adecuara a mis propositos, en Builder 5 con office XP y Win Xp trabaja correctamente, pero con Builder 6 en windows XP y Office 2003 manda lo siguiente:
al ejecutar la función salta el mensage de error:
| Código: | Project proyecto.exe raised exception class EOleSysError with message 'Tipo de vbariable incorrecto'. Process stopped. Use Step or Run to continue. |
// y luego se detiene en esta linea en sysvari.h
| Código: | return OlePropertyGet(name, static_cast<TAutoArgsBase*>(&args));v |
No tengo idea de que pueda ser, en el Buider5 me pedia una libreria la cual agregue al proyecto y jalo, en el Buider 6 hize lo mismo pero quedó igual. Como siempre gracias por la ayuda _________________ - RexL7-
Aprende y enseña:
"Hoy más que ayer, y mucho menos que mañana.." |
|
| Volver arriba |
|
 |
jotny Master


Registrado: 17 Feb 2004Mensajes: 226Ubicación: santiago
|
Publicado: Lun 24 May, 2004 2:30 am Asunto: |
|
|
El error indicaba que el parametro que indica la columna de la hoja no puede ser ANSISTRING, por lo que hay que hacer una conversion de tipos solamente y pasarle los parametros... aqui pongo un ejemplo para exportar desde grid a una excel.... logicamente modificando el codigo para un dataSet,....... hay que hacerle unos pequeños ajustes solamente para que no repita la primera fila del grid... pero es lo menos...
void __fastcall TForm1::grid_a_excel(TStringGrid * qry){
Variant vExcel, vHoja, vRango;
vExcel=CreateOleObject("Excel.Application");
vExcel.OlePropertySet("Visible",true);
vExcel.OlePropertyGet("Workbooks").OleProcedure("Add");
vHoja=vExcel.OlePropertyGet("ActiveSheet");
int row = 1;
String COLUMNA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//para los nombres de columna
//cabeceras
for (int i = 0; i < qry->ColCount; i++){
AnsiString fd = qry->Cells[i][0];
AnsiString columna="";
if(i<=25){
columna=COLUMNA.SubString(i+1,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(i>25 && i <=51){
columna="A"+COLUMNA.SubString(i+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna).OleProcedure("Select");
}
if(i>51 && i <=77){
columna="B"+COLUMNA.SubString(i+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(i>77 && i <=103){
columna="C"+COLUMNA.SubString(i+1-78,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
vExcel.OlePropertyGet("ActiveCell").OlePropertySet("Value",fd.c_str());
}
row++;
row++;
int FILA=0;
AnsiString columna="";
while(FILA<qry->RowCount){
for(int col=0;col<qry->ColCount;col++){
if(col<=25){
columna=COLUMNA.SubString(col+1,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>25 && col <=51){//if(col>26 <=52){
columna="A"+COLUMNA.SubString(col+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>51 && col <=77){
columna="B"+COLUMNA.SubString(col+1-52,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>77 && col <=103){
columna="C"+COLUMNA.SubString(col+1-78,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
vExcel.OlePropertyGet("ActiveCell").
OlePropertySet("Value",qry->Cells[col][FILA].c_str());
}
FILA++;
row++;
}
} |
|
| Volver arriba |
|
 |
rayder Master

Registrado: 03 Mar 2004Mensajes: 207
|
Publicado: Mie 02 Jun, 2004 1:53 pm Asunto: |
|
|
Hola gente, precisamente este post es lo que necesitaba, pero ninguno de los códigos que habeis puesto me funciona.
Me explico, en el código de Jotny, me da lo siguiente:
| Cita: |
[C++ Error] Unit1.cpp(45): E2316 'Cells' is not a member of 'TQuery'
[C++ Error] Unit1.cpp(74): E2316 'RowCount' is not a member of 'TQuery'
[C++ Error] Unit1.cpp(75): E2316 'ColCount' is not a member of 'TQuery'
|
en la línea
| Código: |
AnsiString fd = qry->Cells[i][0];
|
lo que es lógico, ya que este ejemplo es para el builder 6, pero en el ejemplo de Touchsoftwares me compila, pero me da un error al incluir el fichero sysvari.h con el siguiente error:
| Cita: |
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
[C++ Error] sysvari.h(2561): E2449 Size of 'args' is unknown or zero
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
|
Practicamente es el mismo problema que comenta RexL7, he tratado de hacer la conversión de tipos, pero no va.
Agradecería cualquier orientación que me podais dar. _________________ Un saludo
Rayder |
|
| Volver arriba |
|
 |
jotny Master


Registrado: 17 Feb 2004Mensajes: 226Ubicación: santiago
|
Publicado: Mie 02 Jun, 2004 5:32 pm Asunto: estas pasando como parametro a la funcion una TQuery? |
|
|
si te das cuenta... esta funcion tiene como parametro una qry pero tipo TStringGrid, yo la modifique para que me sirviera para un stringgdrid o dbgrid,
lo mas seguro es que estes pegando el codigo solamente y no la funcion completamente.... por lo que ademas tienes que poner la funcion como publica en la clase que tienes de tu form en tu .h
Por ello es que te dice que Cells no es miembro de TQuery... porque es del TStringGrid.. |
|
| Volver arriba |
|
 |
Invitado
|
Publicado: Jue 03 Jun, 2004 11:50 am Asunto: |
|
|
Ruego diculpen mi ignorancía.
En efecto era lo que me comentas, jotny, el problema ahora es que persiste el error en la librería sysvari.h
| Cita: |
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
[C++ Error] sysvari.h(2561): E2449 Size of 'args' is unknown or zero
[C++ Error] sysvari.h(2561): E2450 Undefined structure 'TAutoArgs<1>'
|
En otra ocasión se me presentó el mismo problema, pero mirando la ayuda del builder, lo vi, estaba usando:
| Cita: |
#include <oleauto.hpp>
|
que es la del builder 1 y la cambié por:
| Cita: |
#include <Comobj.hpp>
|
con lo que se solucionó, pero ahora la tengo incluida y persiste el problema.
Sinceramente, ya no sé por donde tirar, porgo el codigo completo por si puede ser de ayuda:
FICHERO: Unit.h
| Código: |
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Db.hpp>
#include <DBGrids.hpp>
#include <DBTables.hpp>
#include <Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TDBGrid *DBGrid1;
TDataSource *DataSource1;
TButton *Button1;
TQuery *qry;
TTable *Table1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::grid_a_excel(TStringGrid * qry);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
|
FICHERO: Unit.cpp
| Código: |
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
// librerias necesarias para trabajar con excel
#include <Comobj.hpp> // para formater las celdas de excel
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Excel_2K_SRVR"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::grid_a_excel(TStringGrid * qry)
{
Variant vExcel, vHoja, vRango;
vExcel=CreateOleObject("Excel.Application");
vExcel.OlePropertySet("Visible",true);
vExcel.OlePropertyGet("Workbooks").OleProcedure("Add");
vHoja=vExcel.OlePropertyGet("ActiveSheet");
int row = 1;
String COLUMNA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//para los nombres de columna
//cabeceras
for (int i = 0; i < qry->ColCount; i++){
AnsiString fd = qry->Cells[i][0];
AnsiString columna="";
if(i<=25){
columna=COLUMNA.SubString(i+1,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(i>25 && i <=51){
columna="A"+COLUMNA.SubString(i+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna).OleProcedure("Select");
}
if(i>51 && i <=77){
columna="B"+COLUMNA.SubString(i+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(i>77 && i <=103){
columna="C"+COLUMNA.SubString(i+1-78,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
vExcel.OlePropertyGet("ActiveCell").OlePropertySet("Value",fd.c_str());
}
row++;
row++;
int FILA=0;
AnsiString columna="";
while(FILA<qry->RowCount){
for(int col=0;col<qry->ColCount;col++){
if(col<=25){
columna=COLUMNA.SubString(col+1,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>25 && col <=51){//if(col>26 <=52){
columna="A"+COLUMNA.SubString(col+1-26,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>51 && col <=77){
columna="B"+COLUMNA.SubString(col+1-52,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
if(col>77 && col <=103){
columna="C"+COLUMNA.SubString(col+1-78,1)+IntToStr(row);
vExcel.OlePropertyGet("Range",columna.c_str()).OleProcedure("Select");
}
vExcel.OlePropertyGet("ActiveCell").
OlePropertySet("Value",qry->Cells[col][FILA].c_str());
}
FILA++;
row++;
}
}
|
|
|
| Volver arriba |
|
 |
rayder Master

Registrado: 03 Mar 2004Mensajes: 207
|
Publicado: Jue 03 Jun, 2004 11:55 am Asunto: |
|
|
Mil perdones, se me olvidó que estaba como invitado, el autor del post de arriba soy yo.
Muchas gracias por la ayuda prestada.
Un saludo |
|
| Volver arriba |
|
 |
|
|
No puedes publicar temas nuevos en este foro No puedes responder a temas en este foro No puedes editar tus mensajes en este foro No puedes borrar tus mensajes en este foro No puedes votar en las encuestas de este foro
|
© 2002–2026 Club Builder · Funciona con phpBB.
|