unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, JvComponentBase, JvDBGridExport,ExcelXP, OleServer;
type
TForm1 =
class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Button2: TButton;
SaveDialog1: TSaveDialog;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
s:
string;
excel: TExcelApplication;
wb: _WorkBook;
ws: _WorkSheet;
lcid: INTEGER;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
lcid := GetUserDefaultLCID;
excel := TExcelApplication.Create(Form1);
excel.Connect;
opendialog1.Filter:= '
Exceldateien (*.xls)|*.XLS';
IF not opendialog1.Execute
THEN exit;
s:= opendialog1.FileName;
// Exceldatei laden (test.xls)
wb := excel.Workbooks.Open(s, emptyParam, emptyParam, emptyParam, emptyParam,
emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam,
emptyParam, emptyParam,emptyParam, emptyParam,lcid);
// erstes Worksheet auswählen
ws := wb.Sheets[1]
as _WorkSheet;
// Feld A1 ausgeben
ShowMessage(ws.Cells.Item[2,1].TEXT);
//Zeile:Spalte
// Hier in dein Grid übergeben
//...
wb.Close(FALSE, emptyParam, emptyParam, lcid);
excel.Quit;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
lcid := GetUserDefaultLCID;
excel := TExcelApplication.Create(Form1);
excel.Connect;
Savedialog1.Filter:= '
Exceldateien (*.xls)|*.XLS';
IF not Savedialog1.Execute
THEN exit;
s:= savedialog1.FileName;
// Exceldatei laden (test.xls)
wb := excel.Workbooks.Add(emptyParam,lcid);
ws := wb.Sheets[1]
as _WorkSheet;
ws.Cells.Item[1,1]:='
Hallo';
ws.SaveAs(s, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam, emptyParam);
wb.Close(FALSE, emptyParam, emptyParam, lcid);
excel.Quit;
end;
end.