好友
阅读权限40
听众
最后登录1970-1-1
|
楼主|
冥界3大法王
发表于 2024-2-24 00:03
unit Unit7;
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
TForm7 = class(TForm)
按行读取方式一: TButton;
Edit1: TEdit;
按行分割字符串: TButton;
按行读取方式二: TButton;
StringGrid1: TStringGrid;
表格添加数据: TButton;
c: TButton;
得到txt总行数: TButton;
遍历StringGrid: TButton;
写入空行: TButton;
procedure 按行读取方式一Click(Sender: TObject);
procedure 按行分割字符串Click(Sender: TObject);
procedure 按行读取方式二Click(Sender: TObject);
procedure 表格添加数据Click(Sender: TObject);
procedure cClick(Sender: TObject);
procedure 得到txt总行数Click(Sender: TObject);
procedure 遍历StringGridClick(Sender: TObject);
procedure 写入空行Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form7: TForm7;
implementation
{$R *.dfm}
procedure TForm7.按行读取方式一Click(Sender: TObject);
var
txtlist: TStringList;
m: integer;
begin
txtlist := TStringList.Create;
txtlist.LoadFromFile(Edit1.Text);
for m := 0 to txtlist.Count - 1 do
begin
ShowMessage(txtlist.Strings[m]);
end;
end;
procedure TForm7.表格添加数据Click(Sender: TObject);
var
i, j: integer;
arr: array[0..200, 0..5] of string; //200行
begin
StringGrid1.Cells[1, 0] := '路径';
StringGrid1.Cells[2, 0] := '完成度';
StringGrid1.Cells[3, 0] := '当前思绪';
StringGrid1.Cells[1, 1] := '翠翠1';
StringGrid1.Cells[1, 2] := '艳艳2';
StringGrid1.Cells[1, 3] := '灵灵3';
StringGrid1.Cells[1, 4] := '风风4';
StringGrid1.Cells[1, 5] := '火火5';
StringGrid1.Cells[2, 1] := 'aaa';
StringGrid1.Cells[2, 2] := 'bbb';
StringGrid1.Cells[2, 3] := 'ccc';
StringGrid1.Cells[2, 4] := 'ddd';
StringGrid1.Cells[2, 5] := 'eee';
StringGrid1.Cells[3, 1] := '111';
StringGrid1.Cells[3, 2] := '222';
StringGrid1.Cells[3, 3] := '333';
StringGrid1.Cells[3, 4] := '444';
StringGrid1.Cells[3, 5] := '555';
StringGrid1.ColWidths[0] := 100; //把第一列隐藏即可
StringGrid1.ColWidths[1] := 1100;
StringGrid1.ColWidths[2] := 400;
StringGrid1.ColWidths[3] := 400;
end;
procedure TForm7.遍历StringGridClick(Sender: TObject);
var
i, j: Integer;
begin
for i := 0 to StringGrid1.RowCount - 1 do
begin
for j := 0 to StringGrid1.ColCount - 1 do
begin
ShowMessage('Cell[' + IntToStr(i) + ',' + IntToStr(j) + ']: ' + StringGrid1.Cells[j, i]); // 访问StringGrid1中每个单元格的内容
if StringGrid1.Cells[j, i] = '' then
begin
ShowMessage('读取到的单元格为为空'); //那就写入空行
end;
end;
end;
end;
procedure TForm7.cClick(Sender: TObject);
var
ATextFile: TextFile;
Line: string;
LineNumber: Integer;
begin
AssignFile(ATextFile, 'X:\要读取的文件.txt');
Reset(ATextFile);
try
LineNumber := 1;
for LineNumber := 1 to 3 do
begin
Readln(ATextFile, Line);
ShowMessage(Line);
Writeln('Line ', LineNumber, ': ', Line);
end;
finally
CloseFile(ATextFile);
end;
end;
procedure TForm7.按行分割字符串Click(Sender: TObject);
var
StringList: TStringList; //与分割有关
要分割的字符串: string; //与分割有关
AI: integer; //与分割有关
begin
StringList := TStringList.Create;
try
要分割的字符串 := 'Apple▲Banana▲Orange';
StringList.Delimiter := '▲'; //设置分隔符为▲
StringList.DelimitedText := 要分割的字符串; //使用DelimitedText属性分割字符串
for AI := 0 to StringList.Count - 1 do //遍历列表并打印每个分割后的字符串
begin
ShowMessage(StringList[AI] + '+' + AI.ToString);
end;
finally
StringList.Free;
end;
end;
procedure TForm7.按行读取方式二Click(Sender: TObject);
var
txtFile: TextFile;
s: string;
begin
AssignFile(txtFile, 'D:\d.txt');
Reset(txtFile);
while not eof(txtFile) do
begin
readln(txtFile, s);
ShowMessage(s);
end;
end;
procedure TForm7.写入空行Click(Sender: TObject);
var
TextFile: TStreamWriter;
begin
// 创建或打开要写入的.txt文件
TextFile := TStreamWriter.Create('example.txt', False);
try
// 写入空行
TextFile.WriteLine(); //空行这么写?
finally
TextFile.Free;
end;
end;
procedure TForm7.得到txt总行数Click(Sender: TObject);
var
A_TextFile: TextFile;
总行数: Integer;
读取到的行内容: string;
begin
AssignFile(A_TextFile, '要读取的文件.txt');
Reset(A_TextFile);
总行数 := 0;
try
while not Eof(A_TextFile) do
begin
Readln(A_TextFile, 读取到的行内容);
Inc(总行数);
end;
// Writeln('Total number of lines in a.txt: ', LineCount);
ShowMessage('总行数是:' + 总行数.ToString);
finally
CloseFile(A_TextFile);
end;
end;
end.
|
|