線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2594
推到 Plurk!
推到 Facebook!

txt檔如何載入StringGrid

 
hanbrother1234
一般會員


發表:8
回覆:2
積分:2
註冊:2006-09-20

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-10-14 14:45:12 IP:140.126.xxx.xxx 未訂閱
我的txt檔是用TAB分隔的浮點數 請問如何載入到StringGrid裡呢?爬過文 但是都看不太懂 請問大大有現成的code嗎
flyup
資深會員


發表:280
回覆:508
積分:385
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-10-19 23:26:26 IP:219.70.xxx.xxx 未訂閱
procedure ReadTabFile(FN: TFileName; FieldSeparator:
Char; SG: TStringGrid);
var 
  i: Integer; 
  S: string; 
  T: string; 
  Colonne, ligne: Integer; 
  Les_Strings: TStringList; 
  CountCols: Integer; 
  CountLines: Integer; 
  TabPos: Integer; 
  StartPos: Integer; 
  InitialCol: Integer; 
begin 
  Les_Strings := TStringList.Create; 
  try 
    // Load the file, Datei laden 
    Les_Strings.LoadFromFile(FN);         // Get the number of rows, Anzahl der Zeilen ermitteln 
    CountLines := Les_Strings.Count   SG.FixedRows;         // Get the number of columns, Anzahl der Spalten ermitteln 
    T := Les_Strings[0]; 
    for i := 0 to Length(T) - 1 do Inc(CountCols,
    Ord(IsDelimiter(FieldSeparator, T, i)));
    Inc(CountCols, 1   SG.FixedCols);         // Adjust Grid dimensions, Anpassung der Grid-Gro?e 
    if CountLines > SG.RowCount then SG.RowCount := CountLines; 
    if CountCols > SG.ColCount then SG.ColCount := CountCols;         // Initialisierung 
    InitialCol := SG.FixedCols - 1;
    Ligne := SG.FixedRows - 1;         // Iterate through all rows of the table 
    // Schleife durch allen Zeilen der Tabelle 
    for i := 0 to Les_Strings.Count - 1 do 
    begin 
      Colonne := InitialCol; 
      Inc(Ligne); 
      StartPos := 1; 
      S := Les_Strings[i]; 
      TabPos := Pos(FieldSeparator, S); 
      repeat 
        Inc(Colonne); 
        SG.Cells[Colonne, Ligne] := Copy(S, StartPos, TabPos - 1); 
        S := Copy(S, TabPos   1, 999); 
        TabPos := Pos(FieldSeparator, S); 
      until TabPos = 0; 
    end; 
  finally 
    Les_Strings.Free; 
  end; 
end;     // Example, Beispiel:     procedure TForm1.Button1Click(Sender: TObject); 
begin 
  Screen.Cursor := crHourGlass; 
  // Open tab-delimited files 
  ReadTabFile('C:TEST.TXT', #9, StringGrid1); 
  Screen.Cursor := crDefault; 
end;
資料來源: http://www.delphirus.com.ru/modules.php?name=Pages&pa=showpage&pid=92
系統時間:2024-03-29 1:29:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!