Forum
Grundkurs
»Introduktion
»Snabbguide
»Komponenter
»Händelser
»Strängar
»Räkna med Delphi »Egna
typer
»Selektion
»Iteration
»Menyer
»Funktioner
»Arraystrukturer
Tips & Tricks
»Nya tips
»Blandat
»Databaser
»Filer
»Forms
»Grafik
»Internet
»Komponenter
»Matematik
»Multimedia
»Objekt/ActiveX
»Skrivare
»Strängar
»System
»Mest lästa tips
Artiklar
»Delphi och ADO
»Bygga en DLL
»Skapa en enkel rapport
»Hantera registret
»Enheter, units
»Klassen TCanvas
»Använd LookUp Controls
Nya
tips
Lägg
till tips
Delphilänkar
Gästbok
|
|
|
|
Manipulera Excel
|
Kategori: Objekt/ActiveX
Inlagt: 2002-10-04
Läst: 1754
Inlagt av: Staffan Berg
|
Beskrivning |
Detta exempel kopierar innehållet i en TStringgrid till ett OLEObject Excel kalkylblad.
|
Kod |
procedure TForm1.OleContainer1Activate(Sender: TObject);
var ExcelSheet : Variant;
Count, Curent : Variant;
i,j : Integer;
Begin
//first we read how many sheets are open in a specified
Excel document
Count:=OleContainer1.OleObject.Application.Sheets.Count;
//then we read the number of a sheet to witch user wants
to add StringGrid content.
Curent:=StrToInt(OKBottomDlg.Edit2.Text);
If Curent <> 0 Then
If Curent <= Count then
// if the sheet with index Curent exist then copy content
Begin
//first we activate the desiered sheet object
OleContainer1.OleObject.Application.Sheets[Count].Activate;
//pass the object to a variant variable
ExcelSheet:=OleContainer1.OleObject.Application.ActiveSheet;
//now we can do what ever we like with it
ExcelSheet.Name:=OKBottomDlg.Edit3.Text+IntToStr(Count);
For i:=0 To StringGrid1.RowCount Do
For j:=0 To StringGrid1.ColCount Do
ExcelSheet.Cells(i,j):=StringGrid1.Cells[j,i];
// here we copy the content
End
else // else if the sheet we are trying to access doesn't exsist
Begin
// we add new sheets untill the requested
// user's index is reached ( curent variable )
For i:=Count+1 To Curent do
OleContainer1.OleObject.Application.Sheets.Add;
// again we do as above
OleContainer1.OleObject.Application.Sheets[Curent].Activate;
ExcelSheet:=OleContainer1.OleObject.Application.ActiveSheet;
ExcelSheet.Name:=OKBottomDlg.Edit3.Text+IntToStr(Count);
For i:=0 To StringGrid1.RowCount Do
For j:=0 To StringGrid1.ColCount Do
ExcelSheet.Cells(i,j):=StringGrid1.Cells[j,i];
End;
end;
|
|
|