La Web del Programador: Comunidad de Programadores
 
    Pregunta:  42730 - AGREGAR UNA HOJA A UNA PLANILLA DE EXCEL
Autor:  Alejandro Damiani
Hola, necesito saber como agregar una hoja a un archivo excel desde delphi.

Muchas gracias.

  Respuesta:  Tania Patricia Mendez Vargas
Bueno aqui te va un procedimiento que te puede servir:

procedure TFRMListadoMaterialComponente.ExportarGrid;
begin
SDexportar.FileName := 'Listado de Componente de Material Oficina.xls';
if SDexportar.Execute then
begin
try
GRListadoRevision.SaveToXLS(SDexportar.FileName);
except
MostrarMensaje(VERSION, 'No ha sido posible realizar la exportación', ICO_ERROR);
end;
end;
end;

  Respuesta:  La liebre De MArzo
procedure TFrm_C_Clientes.Button6Click(Sender: TObject);
var
WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, K, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
begin
if Sag_orden_visita.Cells[0,1] <> '' then
begin
IIndex := 1;
R := Sag_orden_visita.RowCount;
C := Sag_orden_visita.ColCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);
I := 0;
// Define the loop for filling in the Variant
repeat
for J := 0 to (C - 1) do
TabGrid[I,J] := Sag_orden_visita.Cells[J,I];
Inc(I,1);
until
I > (R - 1);

// Connect to the server TExcelApplication
XLApp.Connect;
// Add WorkBooks to the ExcelApplication
XLApp.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := XLApp.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value := TabGrid;
// Customise the WorkSheet
WorkSheet.Name := 'Customers';
Worksheet.Columns.Font.Bold := True;
Worksheet.Columns.HorizontalAlignment := xlRight;
WorkSheet.Columns.ColumnWidth := 14;
// Customise the first entire Column
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
// Show Excel
XLApp.Visible[0] := True;
// Disconnect the Server
XLApp.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end;
end;