Bonjour à tous,
Voici mon code qui me permet de récupéré des données d'un fichier Csv puis de créer un tableaux type RTF dans un TRichEdit.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 private { Private declarations } { GetColCsv } function GetColCsv(const Str: String): Integer; { TabCsvToRTF } procedure TabCsvToRTF(RichEdit: TRichEdit); { LoadCSVtoRTF } procedure LoadCSVtoRTF(FileName: String; RichEdit: TRichEdit); end; var FOpenCsv: TFOpenCsv; TabCsv: array of array of String; const QuoteChar = '"'; Delimiter = ';'; implementation {$R *.dfm}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 { GetColCsv } function TFOpenCsv.GetColCsv(const Str: String): Integer; var InWord, InQuote: Boolean; I, ICount: Integer; begin if Str = EmptyStr then Exit; Result := 1; InWord := False; InQuote := False; for I := 1 to Length(Str) do begin if Str[I] = QuoteChar then InQuote := not InQuote; if Str[I] = Delimiter then begin if not InQuote then Inc(Result); end; end; end;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 { TabCsvToRTF } procedure TFOpenCsv.TabCsvToRTF(RichEdit: TRichEdit); var I, J: Integer; TabStr: String; begin TabStr := '{\rtf1\trowd'; for J := Low(TabCsv[1]) to High(TabCsv[1]) do begin TabStr := TabStr + '\intbl '; for I := Low(TabCsv) to High(TabCsv) do TabStr := TabStr + TabCsv[I, J] + '\cell '; TabStr := TabStr + '\row'; end; TabCsv := nil; with RichEdit do begin Lines.BeginUpdate; SelText := TabStr + '}'; Lines.EndUpdate; end; end;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 { LoadCSVtoRTF } procedure TFOpenCsv.LoadCSVtoRTF(FileName: String; RichEdit: TRichEdit); var StrFile: TStringList; Row, Col, ColCount, IDelimiter: Integer; StrLine: String; begin StrFile := TStringList.Create; try StrFile.LoadFromFile(FileName); if StrFile.Count = 0 then begin FreeAndNil(StrFile); ShowMessage('Le fichier csv ne contient aucune donnée !'); Exit; end; ColCount := GetColCsv(StrFile[0]); if ColCount <= 1 then begin FreeAndNil(StrFile); ShowMessage('Le format du fichier csv n''est pas correct !'); Exit; end; SetLength(TabCsv, ColCount, StrFile.Count); for Row := 0 to StrFile.Count - 1 do begin StrLine := StrFile[Row]; Col := 0; while StrLine <> EmptyStr do begin if Pos(QuoteChar, StrLine) = 1 then begin Delete(StrLine, 1, 1); TabCsv[Col, Row] := Copy(StrLine, 1, Pos(QuoteChar, StrLine) - 1); Delete(StrLine, 1, Pos(QuoteChar, StrLine)); Delete(StrLine, 1, Pos(Delimiter, StrLine)); end else begin IDelimiter := Pos(Delimiter, StrLine); if IDelimiter = 0 then Inc(IDelimiter, Length(StrLine) + 1); TabCsv[Col, Row] := Copy(StrLine, 1, IDelimiter - 1); Delete(StrLine, 1, IDelimiter); end; Inc(Col); end; end; except ShowMessage('Le format du fichier csv n''est pas correct !'); end; FreeAndNil(StrFile); TabCsvToRTF(RichEdit); end;Tout fonctionne impeccable sous Windows 10 par contre sur Windows 7 impossible de créer le tableaux avec les données.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 { OpneCsv Click } procedure TFOpenCsv.OpneCsvClick(Sender: TObject); begin if OpenDialog.Execute() then begin TabCsv := nil; LoadCSVtoRTF(OpenDialog.FileName, RichEdit); end; end;
Résultat sous Windows 10 :
Résultat sous Windows 7 :
Par contre créer un tableau sans données cela fonctionne bien sous Win 7 & 10.
Je n'arrive pas à capté d'où vient de problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 { CmToTwip } function CmToTwip(Cm: Integer): Integer; const Twip1Cm = 566.92; begin Result := Round(Twip1Cm * Cm); end; { MmToTwip } function MmToTwip(Mm: Integer): Integer; const Twip1Mm = 56.69; begin Result := Round(Twip1Mm * Mm); end; { TabRTF } function TFTabStr.TabRTF(TabCol, TabLn, TabSizeCol, TabSizeLn: Integer): String; var I, J: Integer; begin Result := '{\rtf1\trowd'; for I := 1 to TabCol do Result := Result + Format('\cellx%d', [I * (CmToTwip(TabSizeCol))]); for J := 1 to TabLn do begin Result := Result + '\trrh' + IntToStr(MmToTwip(TabSizeLn)) + '\intbl'; for I := 1 to TabCol do Result := Result + '\cell'; Result := Result + '\row'; end; Result := Result + '}'; end; // 10 Col, 100 Ln, 5 cm, 10mm // RichEdit.SelText := TabRTF(10, 100, 5, 10);
Un problème de syntaxe RTF ? ! Cela ne fonctionnerait pas sous Windows 10
Csv.zip
Merci.
Partager