文本文件怎么导入到数据库
文本文件如何导入到数据库?
文本说明:
单据号(20) 商品行数(4) 客户名称(50) 税号(15) 地址(50) 银行帐号(50)
货物名称(30) 计量单位(6) 规格(16) 数量(16.6) 金额(14.2)
文本内容:
单据号 商品行数 单位名称 税号 地址电话 银行帐号
财务软件 套 四模块 2 20000.00
财务软件 套 八模块 2 41000.00
软磁盘 盒 5 " 10 400.00
软磁盘 盒 3.5 " 100 6000.00
硬磁盘 块 1G 1 1300.00
如何读取出来,并填入到数据库?
求高手给预帮帮忙?
------解决方案--------------------
1.读取该文本文件
2.依次保存记录到数据库(就不说了)
下面是以前写过的读取CSV类.可以读取换行过的记录,且对引号做过处理.
type
TCSVRead = class
private
FDataList: TList;
FCurrent: Integer;
function unUsualRow(sLine: string): boolean;
function LoadHead(stCSV: TStringList): boolean;
function GetData: TStringList;
function GetCount: Integer;
public
Head: TStringList;
property Data: TStringList read GetData;
property Count: integer read GetCount;
Constructor Create;
Destructor Destroy;override;
procedure LoadCSV(FileName: string);
function First: boolean;
function Next: boolean;
function Eof: Boolean;
end;
//实现
{ TCSVRead }
constructor TCSVRead.Create;
begin
FDataList := TList.Create;
Head := TStringList.Create;
end;
destructor TCSVRead.Destroy;
var
I: integer;
begin
if Assigned(Head) then
Head.Free;
for I := 0 to FDataList.Count - 1 do
TStringList(FDataList[I]).Free;
FDataList.Free;
inherited;
end;
function TCSVRead.Eof: Boolean;
begin
if FCurrent > = Count then
Result := true
else
Result := false;
end;
function TCSVRead.First: boolean;
begin
if Count > 0 then
FCurrent := 0;
Result := true;
end;
function TCSVRead.GetCount: Integer;
begin
Result := FDataList.Count;
end;
function TCSVRead.GetData: TStringList;
begin
try
if (FCurrent > = 0) and (FCurrent < Count) then
Result := FDataList[FCurrent]
else
Result := nil;
except
Result := nil;
end;
end;
procedure TCSVRead.LoadCSV(FileName: string);
var
stCSV: TStringList;
I,Index,Count: integer;
st: TstringList;
sLine: string;
LineEOF,bReadNext: boolean;
begin
if Trim(FileName) = ' ' then
raise SysUtils.Exception.Create( '请指定CSV文件 ');
try
FDataList.Clear;
FCurrent := -1;
try
stCSV := TStringList.Create;
stCSV.LoadFromFile(Filename);
except
//无法读取文件
end;
Count := stCSV.Count;
if Count > 1 then
LoadHead(stCSV);
I := 1;
while I < Count do begin
st := TstringList.Create;
LineEOF := false;
bReadNext := true;
sLine := stCSV[I];
while not LineEOF do begin
//判断本行格式是不是不正常
if unUsualRow(sLine) then
bReadNext := false;
if sLine[1] = ' " ' then begin
sLine := copy(sLine,2,length(sLine));
repeat begin
Index := pos( ' ", ',sLine);
文本说明:
单据号(20) 商品行数(4) 客户名称(50) 税号(15) 地址(50) 银行帐号(50)
货物名称(30) 计量单位(6) 规格(16) 数量(16.6) 金额(14.2)
文本内容:
单据号 商品行数 单位名称 税号 地址电话 银行帐号
财务软件 套 四模块 2 20000.00
财务软件 套 八模块 2 41000.00
软磁盘 盒 5 " 10 400.00
软磁盘 盒 3.5 " 100 6000.00
硬磁盘 块 1G 1 1300.00
如何读取出来,并填入到数据库?
求高手给预帮帮忙?
------解决方案--------------------
1.读取该文本文件
2.依次保存记录到数据库(就不说了)
下面是以前写过的读取CSV类.可以读取换行过的记录,且对引号做过处理.
type
TCSVRead = class
private
FDataList: TList;
FCurrent: Integer;
function unUsualRow(sLine: string): boolean;
function LoadHead(stCSV: TStringList): boolean;
function GetData: TStringList;
function GetCount: Integer;
public
Head: TStringList;
property Data: TStringList read GetData;
property Count: integer read GetCount;
Constructor Create;
Destructor Destroy;override;
procedure LoadCSV(FileName: string);
function First: boolean;
function Next: boolean;
function Eof: Boolean;
end;
//实现
{ TCSVRead }
constructor TCSVRead.Create;
begin
FDataList := TList.Create;
Head := TStringList.Create;
end;
destructor TCSVRead.Destroy;
var
I: integer;
begin
if Assigned(Head) then
Head.Free;
for I := 0 to FDataList.Count - 1 do
TStringList(FDataList[I]).Free;
FDataList.Free;
inherited;
end;
function TCSVRead.Eof: Boolean;
begin
if FCurrent > = Count then
Result := true
else
Result := false;
end;
function TCSVRead.First: boolean;
begin
if Count > 0 then
FCurrent := 0;
Result := true;
end;
function TCSVRead.GetCount: Integer;
begin
Result := FDataList.Count;
end;
function TCSVRead.GetData: TStringList;
begin
try
if (FCurrent > = 0) and (FCurrent < Count) then
Result := FDataList[FCurrent]
else
Result := nil;
except
Result := nil;
end;
end;
procedure TCSVRead.LoadCSV(FileName: string);
var
stCSV: TStringList;
I,Index,Count: integer;
st: TstringList;
sLine: string;
LineEOF,bReadNext: boolean;
begin
if Trim(FileName) = ' ' then
raise SysUtils.Exception.Create( '请指定CSV文件 ');
try
FDataList.Clear;
FCurrent := -1;
try
stCSV := TStringList.Create;
stCSV.LoadFromFile(Filename);
except
//无法读取文件
end;
Count := stCSV.Count;
if Count > 1 then
LoadHead(stCSV);
I := 1;
while I < Count do begin
st := TstringList.Create;
LineEOF := false;
bReadNext := true;
sLine := stCSV[I];
while not LineEOF do begin
//判断本行格式是不是不正常
if unUsualRow(sLine) then
bReadNext := false;
if sLine[1] = ' " ' then begin
sLine := copy(sLine,2,length(sLine));
repeat begin
Index := pos( ' ", ',sLine);