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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| procedure ExcelToCsv(sCheminFicExcel, sCheminFicCSV: PChar;
fsFormAttente : TForm;
sSeparateur : PChar = PChar(';');
bTerminerParUnSeparateur : boolean = False;
bPrendreEnComptePremiereLigne: boolean = False;
bSeparateurSurLaPremiereLigne: boolean = True);
var
sl : TStringList;
iLigne, iCol, iMax_col : integer;
s:string;
sMonSeparateur : string;
begin
// showmessage('trace toto');
try
Application.ProcessMessages;
Application.CreateForm(TmdTabXl, mdTabXl);
CoInitialize(nil);
lcidExcel := GetUserDefaultLCID;
mdTabXl.EaModeleTabXl.Visible[lcidExcel] := True;
if Assigned(fsFormAttente) then
fsFormAttente.BringToFront;
Application.ProcessMessages;
mdTabXl.EaModeleTabXl.DisplayAlerts[lcidExcel] := False;
mdTabXl.EaModeleTabXl.Connect;
mdTabXl.EwbModeleTabXl.ConnectTo(mdTabXl.EaModeleTabXl.Workbooks.Open(VerifValiditePath(sCheminFicExcel),
emptyparam, emptyparam, emptyparam, emptyparam,
emptyparam, emptyparam, emptyparam, emptyparam,
emptyparam, emptyparam, emptyparam, emptyparam,
lcidExcel));
mdTabXl.EwsModeleTabXl.ConnectTo(mdTabXl.EwbModeleTabXl.Worksheets[1] as _Worksheet);
if FileExists(VerifValiditePath(sCheminFicCSV)) then
if not MessageDlg('Le fichier ' + sCheminFicCSV + ' existe déjà. Voulez vous le remplacer ?', mtWarning, [mbYes, mbNo], 0) = mrYes then
begin
MessageDlg('Opération abandonnée', mtInformation, [mbOK], 0);
exit;
end;
//initialisation de variable
sl := TStringList.Create;
iLigne := 1;
iCol := 1;
iMax_col := 0;
//détermination du nombre de colonnes
while trim(mdTabXl.EwsModeleTabXl.Cells.Item[iLigne,iCol]) <> '' do
begin
inc(iMax_col);
inc(iCol);
end;
//on parcours le fichier excel
if not bPrendreEnComptePremiereLigne then
iLigne := 2;
//s := StrPas(sSeparateur);
sMonSeparateur := ';' ;
if bSeparateurSurLaPremiereLigne then
sl.Append(sMonSeparateur);
while trim(mdTabXl.EwsModeleTabXl.Cells.Item[iLigne,1]) <> '' do
begin
s := '';
for iCol := 1 to iMax_col do
s := s + mdTabXl.EwsModeleTabXl.Cells.Item[iLigne,iCol].Text + ifthen(iCol <> iMax_col, sMonSeparateur, ifthen(bTerminerParUnSeparateur, sMonSeparateur, ''));
sl.Append(s);
inc(iLigne);
end;
sl.SaveToFile(sCheminFicCSV);
mdTabXl.EwbModeleTabXl.Close(True);
mdTabXl.EaModeleTabXl.DisplayAlerts[lcidExcel] := True;
mdTabXl.EaModeleTabXl.Quit;
finally
mdTabXl.EwsModeleTabXl.Disconnect;
mdTabXl.EwbModeleTabXl.Disconnect;
mdTabXl.EaModeleTabXl.Disconnect;
if Assigned(mdTabXl) then
FreeAndNil(mdTabXl);
end;
end; |
Partager