Hi!
Ezzel a megoldással kiteheted Excelbe, csak a formázásokat kell utólag megcsinálni.
Én a LazReport-ot használom jelentés készítésére. Nem rossz, bár vannak hiányosságai. Érdekelne az a Rave-es dolog.
üdv
kbg
var
Form: TForm;
Grid: TDBGrid;
const
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);
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;
procedure ExcelExport(Sender: TObject);
var
FStream: TFileStream;
I, J: Integer;
F: string;
sw_show: longint;
SD: TSavedialog;
begin
SD:= TSavedialog.Create(Application);
SD.FileName:= '';
if SD.Execute then
begin
Screen.Cursor:= crHourGlass;
F:= SD.FileName + '.xls';
FStream := TFileStream.Create(F, fmCreate);
Form:= (((Sender as TComponent).Owner) as TForm);
for i:= 0 to Form.ComponentCount - 1 do
if(Form.Components is TDBGrid) then
Grid:= (Form.Components as TDBGrid);
try
XlsBeginStream(FStream, 0);
for i:= 0 to 0 do
for j:= 0 to Grid.DataSource.DataSet.FieldCount - 1 do
XlsWriteCellLabel(FStream, j, i, Grid.DataSource.DataSet.Fields[j].FieldName);
Grid.DataSource.DataSet.First;
for i:= 0 to Grid.DataSource.DataSet.RecordCount - 1 do
begin
for j:= 0 to Grid.DataSource.DataSet.FieldCount - 1 do
try
case Grid.DataSource.DataSet.Fields[j].DataType of
ftFloat,
ftInteger: XlsWriteCellNumber(FStream, j, i + 1, Grid.DataSource.DataSet.Fields[j].Value);
ftTime: XlsWriteCellLabel(FStream, j, i + 1, TimeToStr(Grid.DataSource.DataSet.Fields[j].Value));
ftDate: XlsWriteCellLabel(FStream, j, i + 1, DateToStr(Grid.DataSource.DataSet.Fields[j].Value));
ftDateTime: XlsWriteCellLabel(FStream, j, i + 1, DateTimeToStr(Grid.DataSource.DataSet.Fields[j].Value));
ftString: XlsWriteCellLabel(FStream, j, i + 1, Trim(Grid.DataSource.DataSet.Fields[j].Text));
end;
except
end;
Grid.DataSource.DataSet.Next;
end;
Grid.DataSource.DataSet.First;
XlsEndStream(FStream);
finally
FStream.Free;
end;
if MessageDlg('Megnyitás', 'Exportálás után megnyitja a listát?',
mtConfirmation,
[mbYes, mbNo],0) = mrYes then
Shellexecute(0, 'open', PChar(SD.FileName + '.xls'), nil, nil, sw_show)
else
ShowMessage('Az exportálás befejeződött' + #13 + F);
end;
SD.Free;
Screen.Cursor:= crDefault;
end;