En este enlace hay un Unit que permite realizar facilmente una exportación de datos a formato excel sin tener excel instalado:
http://cc.embarcadero.com/Item/13462Pero hay que estar registrado en Embarcadero para poder descargar...
Bueno, incluyo ese código con un poco de código de ejemplo:
Este es el código del Unit:
unit Hoja_Calculo;
interface
uses
Classes ;
procedure XlsBeginStream(XlsStream: TStream; const BuildNumber: Word);
procedure XlsEndStream(XlsStream: TStream);
procedure XlsWriteCellLabel(XlsStream: TStream; const ACol, ARow: Word;
const AValue: string);
procedure XlsWriteCellNumber(XlsStream: TStream; const ACol,
ARow: Word; const AValue: Double);
procedure XlsWriteCellRk(XlsStream: TStream; const ACol, ARow: Word;
const AValue: Integer);
var
CXlsBof : array[0..5] of Word = ($809, 8, 00, $10, 0, 0);
CXlsEof : array[0..1] of Word = ($0A, 00);
CXlsLabel : array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
CXlsNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
CXlsRk : array[0..4] of Word = ($27E, 10, 0, 0, 0);
implementation
procedure XlsBeginStream(XlsStream: TStream; const BuildNumber: Word);
begin
// CXlsBof[4] := BuildNumber;
XlsStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;
procedure XlsEndStream(XlsStream: TStream);
begin
XlsStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
end;
procedure XlsWriteCellRk(XlsStream: TStream;
const ACol, ARow: Word;
const AValue: Integer);
var
V: Integer;
begin
CXlsRk[2] := ARow;
CXlsRk[3] := ACol;
XlsStream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
V := (AValue shl 2) or 2;
XlsStream.WriteBuffer(V, 4);
end;
procedure XlsWriteCellNumber(XlsStream: TStream;
const ACol, ARow: Word;
const AValue: Double);
begin
CXlsNumber[2] := ARow;
CXlsNumber[3] := ACol;
XlsStream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
XlsStream.WriteBuffer(AValue, 8);
end;
procedure XlsWriteCellLabel(XlsStream: TStream;
const ACol, ARow: Word;
const AValue: string);
var
L: Word;
begin
L := Length(AValue);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := ARow;
CXlsLabel[3] := ACol;
CXlsLabel[5] := L;
XlsStream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
XlsStream.WriteBuffer(Pointer(AValue)^, L);
end;
end.
Desde un form que tiene un RadioGroup1 (con 3 opciones: decimal, entero y texto) y un Button1.
Este es el evento OnClick de Button1:
procedure TForm1.Button1Click(Sender: TObject);
var
aa : string ;
FStream: TFileStream;
I, J: Integer;
begin
aa := ExtractFilePath( Application.ExeName ) + 'HojaCalculo_01.xls' ;
FStream := TFileStream.Create( aa, fmCreate);
try
XlsBeginStream(FStream, 0);
for I := 1 to strtoint ( Edit2.Text ) do // Columnas
for J := 1 to strtoint ( Edit1.Text ) do // Filas
begin
case RadioGroup1.ItemIndex of
0: XlsWriteCellNumber(FStream, I, J, 34.34);
1: XlsWriteCellRk(FStream, I, J, 3434);
2: XlsWriteCellLabel(FStream, I, J, Format('Cell: %d,%d', [I, J]));
end;
end;
XlsEndStream(FStream);
finally
FStream.Free;
end;
// Si se quiere leer con una hoja de cálculo (por ej. OpenOffice.Org
ShellExecute(Handle,nil, PChar( aa ), '', '',SW_SHOWNORMAL) ;
end;
El Unit se guarda con el nombre que corresponda y se une al proyecto.
Desde el Form1 (que debe tener el Unit en el Uses), se hace click en el button1 y nada mas.
Bueno, espero que a alguien le sea tan útil como me lo ha resultado a mí.
Saludos.
Ramón.