#2
BCLT_xj96052021-11-04 12:48
回复 楼主 BCLT_xj9605
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls, FileCtrl;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
GroupBox1: TGroupBox;
DBGrid1: TDBGrid;
ADOTable1: TADOTable;
DataSource1: TDataSource;
OpenDialog1: TOpenDialog;
ADOConnection1: TADOConnection;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Function RegulateStr(aString:String;Sepchar:String):String;
Function GetSubStr(var aString:String;SepChar:String):string;
Function GetSubStrnum(aString:String;SepChar:String):integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
s,s1:string;
const Space='';
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var //创建时联接并打开数据库显示到DBGrid1中
connectstr:string;
begin
connectstr:=GetCurrentDir+'\GDK.MDB;';
ADOTable1.ConnectionString:=
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+connectstr+'Persist Security Info=False';
ADOTable1.TableName:='a';
ADOTable1.Active:=true;
memo1.Lines.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
var //选择并打开文本文件
f:file of Byte;
l,wz:integer;
begin
AssignFile(f, OpenDialog1.FileName);//打开选择的文件
opendialog1.execute;
Edit1.Text:=OpenDialog1.FileName;
s1:=Edit1.Text;
l:=length(s1);
wz:=LastDelimiter('\',s1);
s1:=copy(s1,wz+1,l-wz);//取目录中井名作s1井号识别。
memo1.Lines.Clear;
memo1.Lines.LoadFromFile(s1);
memo1.ScrollBars:=ssVertical;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
MyLine:String;
begin
//nbsp;
with ADOTable1 do
begin
for i:=0 to memo1.Lines.Count-1 do
begin
MyLine:=RegulateStr(memo1.Lines[i],Space);
//for j:=1 to Num do
//begin
ADOTable1.Append;
ADOTable1.FieldByName('LJSJ').AsFloat:=StrToFloat(GetSubStr(MyLine,Space));
ADOTable1.FieldByName('YL').AsFloat:=StrToFloat(GetSubStr(MyLine,Space));
ADOTable1.Post;
//end;
end;
end;
end;
function TForm1.RegulateStr(aString, Sepchar: String): String;
var
i,Num:Integer;
Flag:Boolean;
MyStr,TempStr:String;
begin
Flag:=False;//进行标志,去除多余的分割符
Num:=Length(aString);//计算aString串的长度
for i:=1 to Num do
begin
TempStr:=Copy(aString,i,1);//取aString串中的一字符
if TempStr<>SepChar then
begin
MyStr:=MyStr+Tempstr;
Flag:=True;
end
else
if(Flag=True)then
begin
Mystr:=Mystr+TempStr;
Flag:=False;
end;
end;
if MyStr[Length(MyStr)]<>SepChar then
MyStr:=MyStr+SepChar;
RegulateStr:=MyStr; //现在的MyStr的内容是文本数据第一行的内容
//showmessage(IntToStr(Num));
//showmessage(RegulateStr); //显示RegulateStr的内容时出错:"Not enough actual parameters"
//exit;
end;
function TForm1.GetSubStr(var aString: String; SepChar: String): string;
var
Mystr:String;
SepCharPos:Integer;
begin
sepCharpos:=pos(sepchar,astring);//计算分割符在子串中的位置
MyStr:=Copy(aString,1,SepCharPos-1);//将分割符前所有字符放到mystr串中
{showmessage(IntToStr(sepCharpos)); //此处得0,???
showmessage(astring);
showmessage(MyStr);
exit;}
Delete(aString,1,SepCharPos);//除去分割符和分割符前的子串
GetSubStr:=MyStr;//返回一个字段
end;
function TForm1.GetSubStrnum(aString, SepChar: String): integer;
var
i:Integer;
StrLen:Integer;
Num:Integer;
begin
StrLen:=Length(aString);//计算aString串的长度
//showmessage(IntToStr(StrLen));
//showmessage(GetSubStr);
//exit;
Num:=0;
for i:=1 to StrLen do
if Copy(aString,i,1)=SepChar then
Num:=Num+1;
GetSubStrNum:=Num;
end;
end.