关于读取TXT文件的问题(200分)

S

skysoft

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手好,我是菜鸟新手,问一个很菜的问题
我这里要读取一个某系统输出的 TXT 文件到 memo 里显示出来,读到memo里是方便有时做些小修改,读到 memo 后输出到 execl 或 word文件中,
整个txt文件要根据内容块分别输出到几个memo中,一般是3 到 9 个memo,每行结束都有一个 | 分隔符
txt文件由系统输出,改不了,文件格式为:
意大利第55轮文字文字文字文字文字文字| 文字文字文字文字文字||文字文字文字文字文字文字|文字文字文字文字文字文字文字文|.省略..|......|.省略.|
英格兰第37轮文字文字| 文字文字文字文字文字||文字文字文字文字文字文字|文字文字文字文字文字文字文字文|.....|.省略..|.省略.|......|
西斑牙第33轮文字| 文字文字文字文字文字||文字文字文字文字文字文字|文字文字文字文字文字文字文字文|.....|.省略..|.省略.|......|
省略.....
我原来用 TstringList 读入,然后一个字一个字,好象很麻烦,很复杂,而且也不知道怎么实现好,如何输出到execl,各位有什么好办法吗, 谢谢
 
意大利第55轮...........
英格兰第37轮..........
西斑牙第33轮文字 ..........
这些各为一个内容块分别存入不同的 memo
 
说个笨方法:
TstringList 一行行读,用POS()找是否有'意大利第','英格兰第','西斑牙第'这些关键字,
再找'轮',取这两个关键字中间的字符,判断是否数字.
找到就 输出到下个MEMO
输出到EXCEL用xlsReadWriteII控件 很好实现.
 
1.分析文本方法,根据楼上的方法写代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;
const
cTitles: array[0..2] of String = ('意大利第','英格兰第','西斑牙第');
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
function ReadTitle(var AText: String): String;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.ScrollBars := ssBoth;
Memo1.Lines.LoadFromFile('c:/1.txt');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i,j: Integer;
s: TStringList;
sTitle,sText: String;
begin
Memo2.Clear;
s := TStringList.Create;
s.Delimiter := '|';
for i := 0 to Memo1.Lines.Count - 1do
begin
s.Clear;
sText := Memo1.Lines;
sTitle := ReadTitle(sText);
s.DelimitedText := sText;
Memo2.Lines.Add('[' + sTitle + ']');
for j := 0 to s.Count - 1do
begin
Memo2.Lines.Add(s.Strings[j]);
end;
Memo2.Lines.Add(' ');
end;
s.Free;
end;

function TForm1.ReadTitle(var AText: String): String;
const
cEnd = '轮';
cEndLen = Length(cEnd);
var
ibegin
,iEnd,i,iStep: Integer;
begin
Result := '';
for i := 0 to High(cTitles)do
begin
ibegin
:= Pos(cTitles,AText);
if ibegin
= 0 then
Continue;
iEnd := PosEx(cEnd,AText,ibegin
+ Length(cTitles));
if iEnd = 0 then
Break;
iStep := iEnd + cEndLen;
Result := Copy(AText,ibegin
,iStep - ibegin
);
AText := Copy(AText,iStep,Length(AText) - iStep + 1);
end;
end;

end.

2.至于要保存为 Excel 也建议用 XLSReadWriteII ,www.2ccc.com 有下载。
 
我已经解决了,还是非常感谢以上两位,废话少说,发钱了,
贴出我的方法,或许有人用得着
procedure TForm3.Button1Click(Sender: TObject);
var
strl:TStringList;
count1:integer;
i:byte;
begin
strl:=Tstringlist.Create;
if opendialog1.Execute then
begin
memo1.Lines.Clear;
memo2.Lines.Clear;
memo3.Lines.Clear;
memo4.Lines.Clear;
memo5.Lines.Clear;
memo6.Lines.Clear;
memo7.Lines.Clear;
memo8.Lines.Clear;
strl.LoadFromFile(opendialog1.FileName);
count1:=strl.Count;
for i:=0 to strl.Count-1do
strl.Strings:=StringReplace(strl.Strings,'|',#13#10,[rfReplaceAll]);
if count1>=3 then
begin
memo1.Lines.Add(strl.Strings[0]);
memo2.Lines.Add(strl.Strings[1]);
memo3.Lines.Add(strl.Strings[2])
end;
if count1>=4 then
memo4.Lines.Add(strl.Strings[3]);
if count1>=5 then
memo5.Lines.Add(strl.Strings[4]);
if count1>=6 then
memo6.Lines.Add(strl.Strings[5]);
if count1>=7 then
memo7.Lines.Add(strl.Strings[6]);
if count1=8 then
memo8.Lines.Add(strl.Strings[7]);
if (count1>8) or (count1<2) then
showmessage('超出范围,请确认文件');
end
else
begin
strl.Free;
showmessage('无文件导入');
exit;
end;
memo8.Lines.Text:=memo1.Lines.Text;
end;
 
顶部