Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #7  
Antiguo 06-08-2008
Avatar de Gaim2205
Gaim2205 Gaim2205 is offline
Miembro
 
Registrado: ago 2007
Ubicación: Durango, Mexico
Posts: 144
Poder: 17
Gaim2205 Va por buen camino
Código Delphi [-]
function TForm1.GetLetraRango(NumCampos: Integer): String;
const
elementos = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
if NumCampos < 27 then
Result:= elementos[NumCampos]
else
begin
Result:=elementos[(NumCampos - 1) div 26];
Result:=Result + elementos[((NumCampos - 1) mod 26)+1];
end;
end;

Código Delphi [-]
procedure TForm1.SendToExcel(Grid: TDBGrid; Forma: TForm);
var
bm: TBookmark;
col, row, renExcel: Integer;
sline, LetraRango, rango: String;
mem: TMemo;
ExcelApp: Variant;
Format : OleVariant;
Evento:TDataSetNotifyEvent;
ToExcel:Boolean;
begin
ToExcel := true;
Screen.Cursor := crHourglass;
Grid.DataSource.DataSet.DisableControls;
bm := Grid.DataSource.DataSet.GetBookmark;
Grid.DataSource.DataSet.First;
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.WorkBooks.Add($FFFFEFB9);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Sheet1';
Grid.DataSource.DataSet.First;
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range['A2:B2'].Select;
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range['A2:B2'].Font.FontStyle := 'Bold';
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range['A2:B2'].Font.Color := clNavy;
mem := TMemo.Create(Self);
mem.Visible := false;
mem.Parent := Forma;
mem.WordWrap := false;
mem.Clear;
sline := '';
for col := 0 to Grid.FieldCount-1 do
sline := sline + Grid.Fields[col].DisplayLabel + #9;
mem.Lines.Add(sline);
if toExcel then
begin
LetraRango := GetLetraRango(Grid.FieldCount);
mem.SelectAll;
mem.CopyToClipBoard;
rango := 'A3:' + LetraRango + '3';
renExcel := 3;
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].Select;
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].Font.FontStyle := 'Bold';
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].Font.Color := clBlack;
ExcelApp.Workbooks[1].WorkSheets['Sheet1'].Paste;
mem.Lines.Clear;
end;
if assigned(Grid.DataSource.DataSet.AfterScroll) then
begin
Evento:=Grid.DataSource.DataSet.AfterScroll;
Grid.DataSource.DataSet.AfterScroll:=nil;
end;
Grid.DataSource.DataSet.DisableControls;
for row := 0 to Grid.DataSource.DataSet.RecordCount-1 do
begin
sline := '';
for col := 0 to Grid.FieldCount-1 do
sline := sline + Grid.Fields[col].AsString + #9;

mem.Lines.Add(sline);
Grid.DataSource.DataSet.Next;

if toExcel then
if mem.Lines.Count = 100 then
begin
mem.SelectAll;
mem.CopyToClipBoard;
rango := 'A' + IntToStr(renExcel+1) + ':' + LetraRango + IntToStr(renExcel+1+mem.Lines.Count-1);
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].Select;
Format := '@';
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].NumberFormat := Format;
ExcelApp.Workbooks[1].WorkSheets['Sheet1'].Paste;
renExcel := renExcel + mem.Lines.Count;
mem.Lines.Clear;
end;
end;
if mem.Lines.Count > 0 then
begin
mem.SelectAll;
mem.CopyToClipboard;
if toExcel then
begin
rango := 'A' + IntToStr(renExcel+1) + ':' + LetraRango + IntToStr(renExcel+1+mem.Lines.Count-1);
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].Select;
Format := '@';
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range[rango].NumberFormat := Format;
ExcelApp.Workbooks[1].WorkSheets['Sheet1'].Paste;
ExcelApp.WorkBooks[1].WorkSheets['Sheet1'].Range['A3:A3'].Select;
ExcelApp.Visible := true;
end;
end;

Grid.DataSource.DataSet.AfterScroll:=Evento;
Grid.DataSource.DataSet.EnableControls;

mem.Free;
Grid.DataSource.DataSet.GotoBookmark(bm);
Grid.DataSource.DataSet.FreeBookmark(bm);
Grid.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;

Código Delphi [-]
SendToExcel(DBGrid1,Form1);

Y necesitas un componente ExcelApplication de la pestaña servers (delphi 7).

Última edición por Gaim2205 fecha: 06-08-2008 a las 18:28:52.
Responder Con Cita
 



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 raf.rsr Servers 42 08-05-2012 20:35:16
Exportar a Excel jocey Varios 3 06-11-2007 16:15:23
Exportar a excel... BuenaOnda Varios 3 20-08-2007 08:55:07
Exportar a Excel erasmorc OOP 4 04-08-2006 15:03:50
Exportar .gdb a Excel Novata2006 Firebird e Interbase 0 16-05-2006 10:52:08


La franja horaria es GMT +2. Ahora son las 20:09:04.


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