Hej alle!
Jeg er ikke så stærk i programering, men vil gerne lege med Excel i Delphi
6!
Jeg har søgt på nettet, men kan ikke rigtig komme vidre.
http://www.swissdelphicenter.ch/torry/showcode.php?id=379
I dette link skulle jeg gerne kunne "export a StringGrid to an Excel-File?"
Det virker ikke???
Skal det installeres et komponent???
Er der nogen der kan hjælpe???
Dette er fejlmeldingen.:
[Error] Unit1.pas(47): Undeclared identifier: 'TStringGrid'
[Error] Unit1.pas(60): 'DO' expected but identifier 'ColCount' found
[Error] Unit1.pas(61): 'DO' expected but identifier 'RowCount' found
[Error] Unit1.pas(67): EXCEPT or FINALLY expected
[Error] Unit1.pas(72): Undeclared identifier: 'Button2Click'
[Error] Unit1.pas(72): ';' expected but '(' found
[Error] Unit1.pas(74): Undeclared identifier: 'StringGrid1'
[Error] Unit1.pas(12): Unsatisfied forward or external declaration:
'TForm1.Button1Click'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
Her er programmet:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, comobj, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// *******************************************
procedure XlsWriteCellLabel(XlsStream: TStream; const ACol, ARow: Word;
const AValue: string);
var
L: Word;
const
{$J+}
CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
{$J-}
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;
function SaveAsExcelFile(AGrid: TStringGrid; AFileName: string): Boolean;
const
{$J+} CXlsBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0); {$J-}
CXlsEof: array[0..1] of Word = ($0A, 00);
var
FStream: TFileStream;
I, J: Integer;
begin
Result := False;
FStream := TFileStream.Create(PChar(AFileName), fmCreate or fmOpenWrite);
try
CXlsBof[4] := 0;
FStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
for i := 0 to AGrid.ColCount - 1 do
for j := 0 to AGrid.RowCount - 1 do
XlsWriteCellLabel(FStream, I, J, AGrid.cells[i, j]);
FStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
Result := True;
finally
FStream.Free;
end;
end;
// Example:
procedure TForm1.Button2Click(Sender: TObject);
begin
if SaveAsExcelFile(StringGrid1, 'c:\MyExcelFile.xls') then
ShowMessage('StringGrid saved!');
end;
// *******************************************
end.
--
--
Mvh
Hans Nikolajsen