读取以下txt:
涟涟 云朵 花儿 朵儿
小翠 小红 小莲 小爱
翠翠 花花 园园 龙龙
翠翠 花花 园园 龙龙
涟涟 云朵 花儿 朵儿
小翠 小红 小莲 小爱
翠翠 花花 园园 龙龙
[Delphi] 纯文本查看 复制代码
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
function SplitString(const source, ch: string): TStringList;
var
temp: string;
i: integer;
begin
result := tstringlist.Create;
temp := source;
i := pos(ch, source);
while i <> 0 do
begin
result.Add(copy(temp, 0, i - 1));
delete(temp, 1, i);
i := pos(ch, temp);
end;
result.Add(temp);
end;
var
i, j, k: integer;
i_RowCount, i_ColCount: Integer;
StrList, TmpList: TStringList;
s_CurItem: string;
begin
StrList := TStringList.Create;
TmpList := TStringList.Create;
StrList.LoadFromFile(ExtractFileDir(ParamStr(0)) + '/test.txt');
for k := 0 to StrList.Count - 1 do
begin
s_CurItem := StrList.Strings[k];
TmpList.Clear;
TmpList := SplitString(s_CurItem, ' ');
i_ColCount := StrList.Count;
i_RowCount := TmpList.Count;
StringGrid1.ColCount := i_RowCount + 1;
for j := 1 to i_RowCount do
begin
StringGrid1.Cells[j, k] := TmpList.Strings[j - 1];
end;
end;
StrList.Free;
TmpList.Free;
end;
|