1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
procedure TMainForm.Button1Click(Sender: TObject);
var xlsxModele : String;
aStream : TResourceStream;
I: Integer;
begin
if Opendialog1.Execute then
begin
Edit1.Text:=Opendialog1.FileName;
with FDConnection1.Params as TFDPhysCDataExcelConnectionDefParams do begin
if not FileExists(OpenDialog1.FileName)
then begin
xlsxModele:=System.IOUtils.TPath.Combine(System.IOUtils.TPath.GetTempPath,'test.xlsx');
aStream:=TResourceStream.Create(HInstance,'xlsx',RT_RCDATA);
aStream.SaveToFile(xlsxModele);
aStream.Free;
ExcelFile:=xlsxModele;
end
else ExcelFile:=OpenDialog1.FileName;
end;
try
FDStoredProc1.ParamByName('file').asString:=OpenDialog1.FileName;
FDStoredProc1.ParamByName('sheet').asString:='un essai';
FDStoredProc1.ParamByName('columnnames').asString:='col1,col2,col3';
FDStoredProc1.ExecProc;
except
on E: EFDDBEngineException do
ShowMessage(E.Message);
end;
// NOTE 3 feuilles sont créées la première avec le nom indiqué, les autres sont nommées Sheet2,Sheet3
FDConnection1.Connected:=False;
with FDConnection1.Params as TFDPhysCDataExcelConnectionDefParams do
ExcelFile:=OpenDialog1.FileName;
end;
// ajout de données
FDQuery1.SQL.Text:='INSERT INTO "un essai" (col1,col2,col3) VALUES (:a,:b,:c)';
for I := 2 to 10 do
begin
FDQuery1.ParamByName('a').DataType:=ftInteger;
Fdquery1.ParamByName('a').asInteger:=i;
Fdquery1.ParamByName('B').asString:=i.ToString;
Fdquery1.ParamByName('c').asString:='Test '+i.ToString;
FDQuery1.ExecSQL;
end;
FDConnection1.Connected:=False; // pour rafraichir
FDQuery1.SQL.Text:='SELECT * FROM "un essai"';
FDQuery1.Active:=True;
end; |
Partager