Tema: Excel
Ver Mensaje Individual
  #4  
Antiguo 30-12-2009
Avatar de santiago14
santiago14 santiago14 is offline
Miembro
 
Registrado: sep 2003
Ubicación: Cerrillos, Salta, Argentina
Posts: 583
Reputación: 21
santiago14 Va por buen camino
Thumbs up

Bueno, aquí pongo un módulo bastante sencillo que permite crear un libro de excel, en este caso le paso dos datasets que son de tipo MDOquery que es similiar a IBX así que no hay que hacerse drama por eso.
también va la dirección física (incluído el nombre del libro .xls) donde quieres poner el nuevo excel.
Lo que si, está bastante pobre en cuanto a funciones que manipulen las celdas propiamente dichas, apelo a la voluntad de la gente del foro para sugerir buenas funciones para todos los chiches que se pueden hacer dentro de las hojas de excel.
Espero te sirva, cualquier cosa me avisas.

Código Delphi [-]

....

implementation

uses
  forms, windows, comObj, excel2000, DB, Math, sysUtils;

.....
procedure exportatExcel(facturas,recibos:TMDOQuery;direccion:string);
var
  Excel, libro, hoja, hojaAnulada: Variant;
  i, iAnulado: integer;
begin
  try
    Excel:=CreateOleObject('Excel.Application');
    Excel.Visible := False;
    Excel.DisplayAlerts:= False;

    Excel.SheetsInNewWorkbook := 4;
  //Cramos un nuevo libro
    libro:=Excel.WorkBooks.Add;
  //Le ponemos nombres a las hojas
    Excel.WorkBooks[1].WorkSheets[1].Name := 'Facturas';
    Excel.WorkBooks[1].WorkSheets[2].Name := 'Facturas Anuladas';

    Excel.WorkBooks[1].WorkSheets[3].Name := 'Recibos';
    Excel.WorkBooks[1].WorkSheets[4].Name := 'Recibos Anulados';

    hoja:=Excel.WorkBooks[1].WorkSheets['Facturas'];
    hojaAnulada:=Excel.WorkBooks[1].WorkSheets['Facturas Anuladas'];
    with facturas do
    begin
    //Ponemos los títulos a las facturas buenas
      hoja.Cells[1,1]:='FECHA'; hoja.Cells[1,2]:='NÚMERO'; hoja.Cells[1,3]:='USUARIO';
      hoja.Cells[1,4]:='IMPORTE'; //hoja.Cells[1,5]:='TIPO'; //hoja.Cells[1,6]:='ESTADO';

    //Ponemos los títulos a las facturas anuladas
      hojaAnulada.Cells[1,1]:='FECHA'; hojaAnulada.Cells[1,2]:='NÚMERO'; hojaAnulada.Cells[1,3]:='USUARIO';
      hojaAnulada.Cells[1,4]:='IMPORTE'; //hojaAnulada.Cells[1,5]:='TIPO'; //hojaAnulada.Cells[1,6]:='ESTADO';

      //open;
      First;
      i:= 2;
      iAnulado:=2;
      while not Facturas.Eof do
      begin
        if Fields[4].AsString = 'PAGADO' then
        begin
          hoja.Cells [i,1] := Fields[0].AsDateTime; //fecha
          hoja.Cells [i,2] := Fields[1].AsString; //nro
          hoja.Cells [i,3] := Fields[2].AsString; //usuario
          hoja.Cells [i,4] := floatToStrF(Fields[5].AsFloat,ffFixed,10,2); //importe
          //hoja.Cells [i,5] := Fields[3].AsString; //tipo
          //hoja.Cells [i,6] := Fields[4].AsString; //estado
          inc(i);
        end
        else
        begin
          hojaAnulada.Cells [iAnulado,1] := Fields[0].AsDateTime; //fecha
          hojaAnulada.Cells [iAnulado,2] := Fields[1].AsString; //nro
          hojaAnulada.Cells [iAnulado,3] := Fields[2].AsString; //usuario
          hojaAnulada.Cells [iAnulado,4] := FloatToStrF(Fields[5].AsFloat,ffFixed,10,2); //importe
          //hojaAnulada.Cells [iAnulado,5] := Fields[3].AsString; //tipo
          //hojaAnulada.Cells [iAnulado,6] := Fields[4].AsString; //estado
          inc(iAnulado);
        end;
        Next;
      end;
      //facturas.Close;
    //Autoajuste de las columnas de la hoja
      hoja.Cells.Columns.AutoFit;
      hojaAnulada.Cells.Columns.AutoFit
    end;

    hoja:=Excel.WorkBooks[1].WorkSheets['Recibos'];
    hojaAnulada:=Excel.WorkBooks[1].WorkSheets['Recibos Anulados'];
    with recibos do
    begin
    //Ponemos los títulos a los recibos buenos
      hoja.Cells[1,1]:='FECHA'; hoja.Cells[1,2]:='NÚMERO'; hoja.Cells[1,3]:='USUARIO';
      hoja.Cells[1,4]:='IMPORTE'; //hoja.Cells[1,5]:='ESTADO';

    //Ponemos los títulos a los recibos anulados
      hojaAnulada.Cells[1,1]:='FECHA'; hojaAnulada.Cells[1,2]:='NÚMERO'; hojaAnulada.Cells[1,3]:='USUARIO';
      hojaAnulada.Cells[1,4]:='IMPORTE'; //hojaAnulada.Cells[1,5]:='ESTADO';

      //open;
      First;
      i:= 2;
      iAnulado:=2;
      while not Recibos.Eof do
      begin
        if Fields[3].AsString = 'PAGADO' then
        begin
          hoja.Cells [i,1] := Fields[0].AsDateTime; //fecha
          hoja.Cells [i,2] := Fields[1].AsString; //nro
          hoja.Cells [i,3] := Fields[2].AsString; //usuario
          hoja.Cells [i,4] := floatToStrF(Fields[4].AsFloat,ffFixed,10,2); //importe
          //hoja.Cells [iAnulado,5] := Fields[3].AsString; //estado
          inc(i);
        end
        else
        begin
        hojaAnulada.Cells [iAnulado,1] := Fields[0].AsDateTime; //fecha
        hojaAnulada.Cells [iAnulado,2] := Fields[1].AsString; //nro
        hojaAnulada.Cells [iAnulado,3] := Fields[2].AsString; //usuario
        hojaAnulada.Cells [iAnulado,4] := floatToStrF(Fields[4].AsFloat,ffFixed,10,2); //importe
        //hojaAnulada.Cells [i,5] := Fields[3].AsString; //estado
        inc(iAnulado);
        end;
        Next;
      end;
      //recibos.Close;
    //Autoajuste de las columnas de la hoja
      hoja.Cells.Columns.AutoFit;
      hojaAnulada.Cells.Columns.AutoFit
    end;
    Libro.SaveAs(direccion);
    Excel.Quit;
  except
    Excel.Quit;
    Application.MessageBox('No se pudo crear el Archivo de Excel.',
      'Backup de Comprobantes',MB_OK + MB_ICONERROR);
    raise;
  end;
end;
__________________
Uno es responsable de lo que hace y de lo que omite hacer.
Responder Con Cita