![]() |
Excelsheets bzw. Excelworkbooks mit Daten füllen
Liste der Anhänge anzeigen (Anzahl: 1)
Zum erfolgreichen Ausführen dieses Quelltextes, bitte folgende Punkte beachten
1. Lege ein neues Projekt an 2. erstelle eine leere Excel Mappe im Verzeichniss C:\ 3. Erstelle auf Dem Formular folgende Komponenten 3.1 ExcelWorksheet1: TExcelWorksheet; 3.2 Button1: TButton; 3.3 ExcelApplication1: TExcelApplication; 3.4 ExcelWorkbook1: TExcelWorkbook; 3.5 Button2: TButton; 3.6 Timer1: TTimer; 4. Stelle folgende Eigenschaften bei den Komponenten ein 4.1 Timer1.Enabled:=False; 4.1 Button1.Caption:='Open Excelsheet and generate dummy values'; 4.1 Button2.Caption:='Close Excelsheet and save'; 5. Erstelle folgende Ereignisbehandlungsroutinen 5.1 procedure Button1Click(Sender: TObject); 5.2 procedure Button2Click(Sender: TObject); 5.3 procedure FormCreate(Sender: TObject); 5.4 procedure Timer1Timer(Sender: TObject); 5.5 procedure ExcelWorkbook1BeforeClose(Sender: TObject; var Cancel: OleVariant); 6. Kopiere den Quelltext aus meiner Demo in dei entsprechenden Ereignisbehandlungsroutinen 7. Speichere das Projekt irgendwo temporaer 8. ... und starte die Applikation 9. Beim Druecken des Button1 wird Excel gestartet und es werden Zufallsdaten erzeugt 10. Beim Druecken des Button2 wird Excel beendet.}
Delphi-Quellcode:
[edit=Matze]Code formatiert. Mfg, Matze[/edit]
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OleServer, Excel2000, StdCtrls, ExtCtrls; type TForm1 = class(TForm) ExcelWorksheet1: TExcelWorksheet; Button1: TButton; ExcelApplication1: TExcelApplication; ExcelWorkbook1: TExcelWorkbook; Button2: TButton; Timer1: TTimer; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure ExcelWorkbook1BeforeClose(Sender: TObject; var Cancel: OleVariant); private { Private-Deklarationen } public { Public-Deklarationen } Counter: Integer; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin // Excel Application starten und Exceldokument oeffnen ExcelApplication1.Connect; ExcelApplication1.Visible[GetUserDefaultLCID] := true; ExcelApplication1.UserControl := True; ExcelApplication1.Workbooks.Open('C:\Mappe1.xls', False, False, EmptyParam, '', False, False, EmptyParam, EmptyParam, false, false, EmptyParam, false, 0); // Workbook verbinden ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkBook); // Workbookeigenschaften einstellen ExcelWorkbook1.Title[1] := 'Programm vom ' + datetimetostr(now); ExcelWorkbook1.Author[1] := 'Specialwork'; ExcelWorkbook1.Comments[1] := 'Dies ist eine Demonstration des Ansprechens von EXCEL Mappen'; ExcelWorkbook1.Save; // Woorksheet verbinden und aktivieren ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets.Item[1] as _Worksheet); ExcelWorksheet1.Activate; // Counter zuruecksetzen Counter := 0; Timer1.Enabled := True; end; procedure TForm1.Button2Click(Sender: TObject); begin Timer1.Enabled := false; ExcelApplication1.Quit; ExcelApplication1.Disconnect; end; procedure TForm1.FormCreate(Sender: TObject); begin // Randomize einschalten randomize; end; procedure TForm1.Timer1Timer(Sender: TObject); var aRange: Range; Value: real; begin if counter = 0 then begin aRange := IDispatch(ExcelWorksheet1.Cells.Item[Counter + 1, 1]) as Range; aRange.Value := timetostr(now); aRange.Show; inc(counter, 1); end else if counter = 1000 then begin aRange := IDispatch(ExcelWorksheet1.Cells.Item[Counter + 1, 1]) as Range; aRange.Value := timetostr(now); aRange.Show; Button2Click(self); exit; end else // Increase Counter inc(Counter, 1); // Get Random Value; Value := random(9999999999999999); Value := Value/1000000; // Insert Random Value aRange := IDispatch(ExcelWorksheet1.Cells.Item[Counter, 1]) as Range; aRange.Value := Value; aRange.Show; end; procedure TForm1.ExcelWorkbook1BeforeClose(Sender: TObject; var Cancel: OleVariant); begin Timer1.Enabled := False; end; end. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz