適用状況:
- CSVデータをListViewに表示する
機能仕様:
| 機能 | 仕様 |
|---|---|
| インクルードユニット | System.Classes // TstringList Vcl.ComCtrls // TListItem |
| 参考記事 | CSVファイルをTListViewに読み込むには? |
procedure load_csv_ListView ;
// ================================================
// csvからデータを取得して、
// ListView1に表示する
// ================================================
const
_file_name : string = 'test.csv' ;
var
_row_list : TStringList ; // ファイルから読み込んだ行データ
_cell_list : TStringList ; // ファイルから読み込んだセルデータ
_list_item : TListItem ; // 読み込んだアイテム
_iRow : Integer ;
_iCell : Integer ;
begin
if not FileExists( _file_name ) then begin
ShowMessage( 'not Exist' ) ;
Exit ;
end ;
_row_list := TStringList.Create ;
_cell_list := TStringList.Create ;
try
// ファイルを読み込み
_row_list.LoadFromFile( _file_name ) ;
// 1行ずつ取り出す-------------------------------------------
for _iRow := 0 to _row_list.Count - 1 do begin
// 1行を各cellに分解
_cell_list.CommaText := _row_list[ _iRow ] ;
// リストビューの追加リストと、_list_itemをリンクさせる
_list_item := ListView1.Items.Add ;
// 1列目
if _cell_list.Count > 0 then
_list_item.Caption := _cell_list[ 0 ] ;
// 2列目以降
if _cell_list.Count > 1 then
for _iCell := 1 to _cell_list.Count - 1 do begin
_list_item.SubItems.Add( _cell_list[ _iCell ] ) ;
end ;//next _iCell
end ;// next _iRow-----------------------------------------
finally
_row_list.Free ;
_cell_list.Free ;
end ;
end ;