Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 06-07-2006
Avatar de Bicho
[Bicho] Bicho is offline
Miembro Premium
 
Registrado: jul 2003
Ubicación: Inca - Mallorca
Posts: 1.776
Poder: 22
Bicho Va por buen camino
Exportar un StringGrid a Excel

Este truco sirve como ampliación al de Neftalí en el de [HTML]<a href=http://www.clubdelphi.com/trucos/index.php?id=38 >Exportar un StringGrid a un Fichero</a>
[/HTML]
Hay que añadir al uses el Excel_TLB, ActiveX.

Y a la función sólo le pasamos el stringgrid que queremos exportar y el rango de columnas y filas que queremos exportar
Código Delphi [-]
procedure ExportaExcel(pStringGrid : TstringGrid; c0,r0,c1,r1 : Integer);
var ExLin, ExCol, i, Linea, AuxInteger : Integer;
    AuxFloat : Double;
    AuxFecha : tDatetime;
    c : TCursor;
    Excel, ExcelDoc, WS : Variant;
begin
  c             := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  with pStringGrid do begin
    try
      coinitialize(nil);
      Excel         := CreateOleObject('Excel.Application');
      ExcelDoc      := Excel.Workbooks.Add;
      WS            := ExcelDoc.ActiveSheet;
      Excel.Visible := true;
      ExCol         := 0;
      for i := c0 to c1 do begin
        inc(ExCol);
        ws.Cells.Item[1, ExCol]:= cells[i, 0];
      end;

      for linea := r0 to r1 do begin
        inc(ExLin);
        ExCol := 0;
        for i := c0 to c1 do begin
          inc(ExCol);
          try
            try
              if pos('/', cells[i, linea - 1]) <> 0 then begin
                AuxFecha := strtodatetime(cells[i, linea - 1]));
                ws.Cells.Item[ExLin, ExCol] := AuxFecha;
              end
              else AuxFecha := strtodatetime('GENERA EXCEPCION');
            except
              try
                AuxInteger := strtoint(cells[i,linea-1]));
                ws.Cells.Item[ExLin,ExCol]:= AuxInteger;
              except
                try
                  AuxFloat := strtofloat(cells[i,linea-1]);
                  ws.Cells.Item[ExLin,ExCol]:= AuxFloat;
                except
                  ws.Cells.Item[ExLin,ExCol]:= cells[i,linea-1];
                end;
              end;
            end;
          except end;
        end;
      end;
      ws.cells.entirecolumn.autofit;
    finally
      screen.Cursor := c;
    end;
  end;
end;

Un ejemplo de uso puede ser:

Código Delphi [-]
ExportaExcel(StringGrid1, 0, StringGrid1.ColCount-1, 0, StringGrid.RowCount-1);

Editado: hay que añadir tambien la uses ActiveX, y la funcion AllTrim la he quitado.
Responder Con Cita
  #2  
Antiguo 27-03-2008
jojobax_ jojobax_ is offline
Registrado
 
Registrado: mar 2008
Posts: 2
Poder: 0
jojobax_ Va por buen camino
necesito la adaptacion de este codigo para borland c++ rapidamente, POR FAVOOOR! Millones de gracias
Responder Con Cita
  #3  
Antiguo 27-03-2008
jojobax_ jojobax_ is offline
Registrado
 
Registrado: mar 2008
Posts: 2
Poder: 0
jojobax_ Va por buen camino
olvide dejar mi e-mail: jojobax_@hotmail.com
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

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


La franja horaria es GMT +2. Ahora son las 22:05:30.


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