PDA

Ver la Versión Completa : tamaño de hoja al exportar datos a excel


edgar_prospero
05-11-2012, 20:00:56
que hay amigos aqui estoy con un problema, estoy mandando un reporte a excel pero quisiera saber su hay la forma de que la hoja quede configurada a la hora de mandar la informacion, me refiero a que ya que tengo la informacion en excel y si le doy en vista previa ya este a a cierto porcentaje el tamaño de la pagina y no haya necesidad de configurarlo manualmente, no se si me explique un poco acontinuacion pongo el codigo que utilizo para mandar el reporte a excel:

procedure TForm5.BitBtn6Click(Sender: TObject);
var Excel : Variant;
var Libro : Variant;
var i:integer;
var j:integer;
var _FilaIni: integer;

begin

Excel := CreateOleObject ('Excel.Application');

Excel.Visible := True;


Excel.WorkBooks.Add (-4167);

Excel.WorkBooks[1].WorkSheets[1].Name := 'Reporte';

Libro := Excel.WorkBooks[1].WorkSheets['Reporte'];
excel.worksheets[1].Range['B1','B1'].ColumnWidth :=45;
excel.worksheets[1].Range['C1','C1'].ColumnWidth :=13;
excel.worksheets[1].Range['D1','E1'].ColumnWidth :=15;
excel.worksheets[1].Range['F1','F1'].ColumnWidth :=16;
excel.worksheets[1].Range['G1','H1'].ColumnWidth :=9;
excel.worksheets[1].Range['I1','I1'].ColumnWidth :=14;
Libro.Cells.Item[1,1].Font.Bold:=True;
Libro.Cells.Item[1,1].font.size:=16;

Libro.Cells.Item[1,3]:='Fecha Inventario: '+ FormatDateTime('dd/mm/yyyy',form5.fecha.Date);
Libro.Cells.Item[1,6]:=' Supervisor: ' +form5.supervisor.Text;
Libro.Cells.Item[2,1]:= ' Inventario en proceso del departamento: '+form5.dep2.Text + ' A ' +form5.dep3.Text+ ' del ' + FormatDateTime('dd/mm/yyyy',form5.inifec.Date)+ ' al ' + FormatDateTime('dd/mm/yyyy',form5.finfec.Date);
Libro.Cells.Item[2,1].Font.Bold:=True;


Libro.Cells.Item[1,3].Font.Bold:=True;
Libro.Cells.Item[1,6].Font.Bold:=True;
Libro.Cells.Item[3,1].Font.Bold:=True;
Libro.Cells.Item[3,2].Font.Bold:=True;
Libro.Cells.Item[3,3].Font.Bold:=True;
Libro.Cells.Item[3,4].Font.Bold:=True;
Libro.Cells.Item[3,5].Font.Bold:=True;
Libro.Cells.Item[3,6].Font.Bold:=True;
Libro.Cells.Item[3,7].Font.Bold:=True;
Libro.Cells.Item[3,8].Font.Bold:=True;
Libro.Cells.Item[3,9].Font.Bold:=True;



Query.Open;
i:=3;

query.SQL.Clear;
query.SQL.Add('SELECT * FROM arm0062 WHERE departamento>=:inidep and departamento<=:findep and tipo_material>=:inimat and tipo_material<=:finmat and fecha>=:inifec and fecha<=:finfec order by codigo asc ');
query.ParamByName('inidep').Asstring:=inidep.Text;
query.ParamByName('findep').Asstring:=findep.Text;
query.ParamByName('inimat').Asstring:=inimat.Text;
query.ParamByName('finmat').Asstring:=finmat.Text;
query.ParamByName('inifec').AsDate:=inifec.Date;
query.ParamByName('finfec').AsDate:=finfec.Date;
query.Open;

while not Query.EOF do
begin

Libro.Cells [1,1] := ' CLASIC AND BASIC SA DE CV';
Libro.Cells.Item[1,1].HorizontalAlignment := xlHAlignleft;
Libro.Cells [3,1] := 'codigo';
Libro.Cells [3,2] := 'Nombre';
Libro.Cells [3,3] := 'Departamento';
Libro.Cells [3,4] := 'Tipo de Material';
Libro.Cells [3,5] := 'Cantidad piezas';
Libro.Cells [3,6] := 'Cantidad Material';
Libro.Cells [3,7] := 'Unidad';
Libro.Cells [3,8] := 'Costo';
Libro.Cells [3,9] := 'Total';

i :=i+1;


Libro.Cells [i,1] := Query.FieldByName ('codigo').AsString;
Libro.Cells [i,2] := Query.FieldByName ('nombre').AsString;
Libro.Cells [i,3] := Query.FieldByName ('departamento').AsString;
Libro.Cells [i,4] := Query.FieldByName ('tipo_material').AsString;
Libro.Cells [i,5] := Query.FieldByName ('cant_piezas').AsString;
Libro.Cells [i,6] := Query.FieldByName ('cant_material').AsString;
Libro.Cells [i,7] := Query.FieldByName ('unidad').AsString;
Libro.Cells [i,8] := Query.FieldByName ('costo').AsString;
Libro.Cells [i,9] := Query.FieldByName ('total').AsString;


Query.Next;

end;

Query.Close;


Libro.Cells.Item[i+1,2].Font.Bold:=True;
Libro.Cells.Item[i+1,9].Font.Bold:=True;

Libro.Cells.Item[i+1,2].HorizontalAlignment := xlHAligncenter;


Libro.Cells [i+1,2] := 'TOTAL';

libro.cells[i+1,9].formula:= format( '=SUM(I4:I%d)', [I] );


DEP2.Text:='PRODUCTO TERMINADO';
DEP3.Text:='ALMACEN TEMPORAL';
inifec.Date:=date;
finfec.Date:=date;
iniFEC.SetFocus;
SUPERVISOR.Clear;
fecha.Date:=date;

end;

nlsgarcia
05-11-2012, 21:52:19
edgar_prospero,

Revisa este link : http://www.swissdelphicenter.ch/torry/showcode.php?id=156

// Este es un fragmento de código del link anterior relacionado al Page Setup
{ Page Setup }

ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;

// Left, Right Margin (Seitenränder)
ExcelApp.ActiveSheet.PageSetup.LeftMargin := 35;
ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;

// Set Footer Margin
ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);

// Fit to X page(s) wide by Y tall
ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1; // Y
ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y

// Zoom
ExcelApp.ActiveSheet.PageSetup.Zoom := 95;

// Set Paper Size:
ExcelApp.PageSetup.PaperSize := xlPaperA4;

// Show/Hide Gridlines:
ExcelApp.ActiveWindow.DisplayGridlines := False;

// Set Black & White
ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;

// footers
ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte Fußzeile';
ExcelApp.ActiveSheet.PageSetup.LeftFooter := 'Left Footer / Linke Fußzeile';

// Show Excel Version:
ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));

// Show Excel:
ExcelApp.Visible := True;

// Save the Workbook
ExcelApp.SaveAs('c:\filename.xls');

// Save the active Workbook:
ExcelApp.ActiveWorkBook.SaveAs('c:\filename.xls');

Nota : El método PageSetup no es soportado en Automation OLE de Excel 2010 según pude constatar al probar el código.

Espero sea útil :)

Nelson.

edgar_prospero
05-11-2012, 23:59:41
excelente respuesta gracias a esto pude solucionar mi problema con la siguiente linea:D

Excel.ActiveSheet.PageSetup.Zoom := 65;