如何保存 TListView 内的内容到文件?

解决方案 »

  1.   

    var
      IniFile: String;
      fSource: TextFile;
    begin
      IniFile := _DIR_APP+__SDefaultDIR;
      if FileExists(IniFile) then  // 存在该 Ini 文件
      begin
          AssignFile(fSource,IniFile);
          ReSet(fSource);
          while not Eof(fSource) do begin
          ReWrite(fSource);
          WriteLn(fSource,text);  // 写内容
          CloseFile(fSource);
      end;
    end;
      

  2.   


    procedure TForm1.Button1Click(Sender: TObject);
    var slt: Tstringlist;
        i,j: integer;
    begin
    slt:= Tstringlist.Create;
    j:=  ListView1.Items.Count;
    for i:=0 to j - 1 do
    slt.Add(ListView1.Items[i].Caption);
    slt.SaveToFile('c:\a.txt');
    end;
      

  3.   

    const
      FormatStr = '%:-20s|';
    var
      StrList: TStringList;
      Str: string;
      Line: string;
      i, j: integer;
    begin
      StrList := TStringList.Create;
      try
        Str := '';
        Line := '';
        for i := 0 to ListView1.Columns.Count - 1 do
        begin
          Str := Str + Format(FormatStr, [ListView1.Columns[i].Caption]);
          Line := Line + '--------------------+';
        end;
        StrList.Add(Str);
        Strlist.Add(Line);
        for j := 0 to ListView1.Items.Count - 1 do
        begin
          Str := Format(FormatStr, [ListView1.Items[j].Caption]);
          for i := 1 to ListView1.Columns.Count - 1 do
            Str := Str + Format(FormatStr, [ListView1.Items[j].SubItems[i - 1]]);
          StrList.Add(Str);
        end;
        Strlist.SaveToFile('temp.txt');
      finally
        StrList.Free;
      end;
    end;