Ver Mensaje Individual
  #10  
Antiguo 12-04-2005
lroblesco lroblesco is offline
Miembro
 
Registrado: ene 2005
Ubicación: Baja California Sur, Mexico
Posts: 77
Reputación: 20
lroblesco Va por buen camino
Exportar a Excel

Hace unos dias pregunte como exportar un stringrid a excel. Encontre un codigo aqui en el foro donde pasa de un ListView a xls, yo lo adapte para mis necesidades. Lo pongo por si otro tiene una problematica similar.
Ojo. Se necesita tener instalado excel.
Código:
 procedure TForm1.Button1Click(Sender: TObject);
 var
   Excel,xlw : Variant;
   fichero : String;
   i,j : integer;
   creado:boolean;
   MisDoc,XLSFILE:string;
 
 begin
 for i:=0 to grid.rowcount do
   for j:=0 to grid.ColCount do
 	grid.Cells[j,i]:=inttostr(i);
  creado:=false;
   try
 	begin
 	 Excel := CreateOLEObject('Excel.Application');
 	 //	Excel.WorkBooks.Add;
 	 xlw:=Excel.WorkBooks.add;
 	  creado:=true;
 	 end;
   except
 	begin
 	 Excel.Quit;
 	  raise;
 	 end;
 end;  //fin try
 
 	//***** RECORREMOS EL LISTVIEW E INSERTAMOS LOS DATOS EN EL OBJETO EXCEL *****//
    try
 	 for i:=0 to grid.rowcount-1 do
 	   for j:=0 to grid.ColCount-1 do
 		 Excel.Cells[i+1,j+1].Value:=grid.Cells[j,i];
    except
 	begin
 	   Excel.Quit;
 	   creado:=false;
 		raise;
 	 end;
    end;//end try
 
 	//***** GUARDAMOS EL OBJETO EXCEL *****//
  if (creado = true) then
   begin
 	TRY
 	 //xlw.SaveAs(FileName:=XLSFILE, Password := '');
 	 xlw.SaveAs('c:\hola.xls', Password := '');
 	  FINALLY
 		Excel.Quit;
 	end;  //fin try
    end
 	else
 	  Excel.Quit;
 
 
 
 
 end;
__________________
No siempre el mas veloz es el que gana la carrera
Responder Con Cita