Приветствую Вас Гость | RSS

Меню сайта

Реклама

Категории раздела
ADO [17]
ASCII и CSV [12]
Access [20]
Alias [24]
BDE [37]
BLOB поля [19]
Clipper [2]
DB2 [2]
DBASE и DBF [26]
Fox Pro [1]
Interbase [21]
MSSQL [0]
ODBC [10]
Oracle [0]
Paradox [0]
SQL [29]
Sybase [1]
База данных [0]
Закладки [2]
Записи [0]
Индексы [10]
Компоненты и Базы данных [0]
Модуль данных [3]
Отчеты [2]
Ошибки БД [17]
Поиск [16]
Поля [0]
Сортировка и Фильтр [6]
Таблицы [0]

Наш опрос
Какие компоненты добавлять больше?
Всего ответов: 49

Статистика

Онлайн всего: 1
Гостей: 1
Пользователей: 0

Форма входа

Главная » Статьи » Базы данных » ASCII и CSV

Читаем CSV текстовый файл в StringGrid
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;
Категория: ASCII и CSV | Добавил: Skinner (03.07.2008)
Просмотров: 493 | Рейтинг: 0.0/0
  Delphi Lab   Главная   Регистрация   Вход  
Интересная Цитата

Поиск

Магазин


Copyright MyCorp © 2025 Хостинг от uCoz