Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Servers (https://www.clubdelphi.com/foros/forumdisplay.php?f=9)
-   -   Cómo insertar datos en la segunda hoja de excel (https://www.clubdelphi.com/foros/showthread.php?t=53692)

Viky 27-02-2008 13:33:40

Cómo insertar datos en la segunda hoja de excel
 
Hola,

Creo un libro de excel del sgte modo:

ExcelApplication1.Workbooks.Add(EmptyParam ,0);

Agrego un título de columna:

RangeE := ExcelApplication1.ActiveCell;
RangeE.Font.FontStyle :='Arial';
RangeE.Font.Size :=10;
RangeE.Value :='General';
RangeE := ExcelApplication1.Range['A' + IntToStr(2), 'A' + IntToStr(2)];
RangeE.Font.FontStyle :='Arial';
RangeE.Font.Size :=10;

Cuando creo el libro ya me crea con 3 hojas.

Mi duda es como inserto datos en la segunda hoja?? porq de este modo estoy trabajando en la primer hoja. De qué manera le digo que quiero por ejemplo en la celda A2 de la segunda hoja colocar el valor 1.


Muchas Gracias
Viky.

cHackAll 27-02-2008 14:14:41

Aquí

Caro 27-02-2008 14:49:22

Hola Viky

Código Delphi [-]
 ExcelWorksheet1.ConnectTo(ExcelApplication1.Sheets[1] as _WorkSheet);//Obtenemos la primera hoja y escribes
 ExcelWorksheet1.Cells.Item[1,1].Value := 'xxxxxxxx';

 ExcelWorksheet1.ConnectTo(ExcelApplication1.Sheets[2] as _WorkSheet);//Obtenemos la segunda hoja y escribes
 ExcelWorksheet1.Cells.Item[1,1].Value := 'yyyyyyy';

Saluditos

lgarcia 27-02-2008 15:04:31

Insercion de datos en varias hojas de Excel
 
Hola:

En este caso te recomiendo trabajar sobre una plantilla de Excel, por que tiene las facilidades de que puedes formatear las columnas con el tipo de datos que vas a pasarle. Aqui te pongo un pequeño ejemplo que copia datos desde 3 StoreProcedure y llena tres hojas de excel en la casillas que uno desee.

Código:

Procedure InsertarExcel;
var
  Excel, Libro: Variant;
  Nombre: string;
  dia, mes: string;
  Year, Month, Day: Word;
begin
        DecodeDate(dtp1.Date, Year, Month, Day);
        dia:= IntToStr(Day);
        mes:= IntToStr(Month);
        RellenarCeros(dia);
        RellenarCeros(mes);
        nombre:= '13' + dia + mes + IntToStr(Year);
        Excel:=CreateOleObject('Excel.Application');
        Excel.Visible := False;
        Excel.DisplayAlerts:= False;
        Excel.WorkBooks.Add('C:\Archivos de programa\PMO\XDDMMAAAA.xlt');
        Excel.WorkBooks[1].WorkSheets[1].Name := 'MONTO DE EFECTIVO CONTADO';
        Libro := Excel.WorkBooks[1].WorkSheets['MONTO DE EFECTIVO CONTADO'];
        Libro.Cells [5,2] := DateToStr(dtp1.Date);
        with Conteo do
        begin
          Close;
          Parameters.ParamByName('@fecha').Value:= Int(dtp1.Date);
          Open;
        end;
        Conteo.First;
        while not (Conteo.Eof) do
        begin
          case Conteoid.Value of
            1:Libro.Cells [5,2] := Conteoimporte.Value;
            2:Libro.Cells [6,2] := Conteoimporte.Value;
            3:Libro.Cells [8,2] := Conteoimporte.Value;
            4:Libro.Cells [16,2] := Conteoimporte.Value;
            5:Libro.Cells [9,2] := Conteoimporte.Value;
            6:Libro.Cells [14,2] := Conteoimporte.Value;
            7:Libro.Cells [11,2] := Conteoimporte.Value;
            8:Libro.Cells [7,2] := Conteoimporte.Value;
            9:Libro.Cells [10,2] := Conteoimporte.Value;
          end;
          Conteo.Next;
        end;
        Excel.WorkBooks[1].WorkSheets[2].Name := 'PUNTOS NEGATIVOS EN EL DÍA';
        Libro := Excel.WorkBooks[1].WorkSheets['PUNTOS NEGATIVOS EN EL DÍA'];
        Libro.Cells [5,2] := DateToStr(dtp1.Date);
        with Puntos do
        begin
          Close;
          Parameters.ParamByName('@fecha').Value:= Int(dtp1.Date);
          Open;
        end;
        Libro.Cells [5,1] := Puntosvia.Value;
        Libro.Cells [5,2] := Puntossp.Value;
        Libro.Cells [5,3] := Puntostpte.Value;
        Libro.Cells [5,4] := Puntosso.Value;
        Libro.Cells [5,5] := 0;
        Libro.Cells [5,6] := Puntoscerrado.Value;
        Libro.Cells [5,7] := Puntosnotiene.Value;
        Libro.Cells [5,8] := Puntosnomat.Value;
        Libro.Cells [5,9] := Puntosnoestaba.Value;
        Excel.WorkBooks[1].WorkSheets[3].Name := 'ENTRADA CARROS';
        Libro := Excel.WorkBooks[1].WorkSheets['ENTRADA CARROS'];
        Libro.Cells [5,2] := DateToStr(dtp1.Date);
      with Rutas do
        begin
          Close;
          Parameters.ParamByName('@fecha').Value:= Int(dtp1.Date);
          Open;
        end;
        Rutas.First;
        while not (Rutas.Eof) do
        begin
          case Rutasorden.Value of
            1:Libro.Cells [5,3] := Rutascantidad.Value;
            2:Libro.Cells [6,3] := Rutascantidad.Value;
            3:Libro.Cells [7,3] := Rutascantidad.Value;
            4:Libro.Cells [8,3] := Rutascantidad.Value;
            5:Libro.Cells [9,3] := Rutascantidad.Value;
          end;
          Rutas.Next;
        end;
        Libro.SaveAs('D:\Partes Habana\' + nombre);
        ShowMessage(' El fichero ' + Nombre +  ' ha sido creado exitosamente');
        Excel.Quit;
      end;
end;

Esperando haberte ayudado en algo.

Saludos

Luis Garcia

hurry_master 19-05-2011 20:02:38

Help
 
oye y para crear las pestañas de manera dinamica, es decir dentro de un while o un for, de antemano gracias


La franja horaria es GMT +2. Ahora son las 08:05:32.

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