unit Form_ToChangCSVforDBFU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, DB, DBTables, Grids, DBGrids, ExtCtrls,
StdCtrls;
type
TForm_ChangCSVToDBFF = class(TForm)
PopupMenu1: TPopupMenu;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
ProgressBar1: TProgressBar;
N1: TMenuItem;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
StatusBar1: TStatusBar;
Button1: TButton;
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_ChangCSVToDBFF: TForm_ChangCSVToDBFF;
implementation
{$R *.dfm}
procedure TForm_ChangCSVToDBFF.N1Click(Sender: TObject);
var
llist,BBList:TStringList;
i,j:Integer;
Fname,Path:string;
begin
self.OpenDialog1.Title :='打开CSV文件';
Self.OpenDialog1.FileName:='*.CSV';
Self.OpenDialog1.Filter :='CSV文件(*.CSV)|*.CSV|所有文件(*.*)|*.*';
if Self.OpenDialog1.Execute then
begin
Self.StatusBar1.Panels[1].Text :=Self.OpenDialog1.FileName;
Self.SaveDialog1.Title :='保存DBF为...';
Self.SaveDialog1.DefaultExt:='DBF';
Self.SaveDialog1.Filter :='dDBASE文件(*.DBF)|*.DBF|所有文件(*.*)|*.*';
Fname :=ExtractFileName(Self.OpenDialog1.FileName);
Fname:=ChangeFileExt(Fname,'');
Self.SaveDialog1.FileName:=Fname+'.dbf';
if Self.SaveDialog1.Execute then
begin
Fname:=ExtractFileName(Self.SaveDialog1.FileName);
Path :=ExtractFilePath(Self.SaveDialog1.FileName);
llist:=TStringList.Create;
llist.LoadFromFile(Self.OpenDialog1.FileName);
BBList:=TStringList.Create;
BBList.Delimiter:=',';
Self.Table1.Active :=False;
Self.Table1.DisableControls;
Self.Table1.DatabaseName:=Path;
Self.Table1.TableName:=Fname;
Self.Table1.TableType :=ttDbase;
Self.Table1.FieldDefs.Clear;//这句很重要
Self.Table1.IndexDefs.Clear;//这句很重要
BBList.DelimitedText:=llist.Strings[0];
Self.Table1.FieldDefs.Add(BBList[0],ftInteger,0,True);
for i:=1 to BBList.Count-1 do
begin
Self.Table1.FieldDefs.Add(BBList[i],ftFloat,0,True);
end;
Self.Table1.CreateTable;
Self.Table1.Active :=True;
Self.ProgressBar1.Min:=0;
Self.ProgressBar1.Max :=llist.Count -1;
for i:=1 to llist.Count-1 do
begin
Self.Table1.Append;
BBList.DelimitedText:=llist.Strings[i];
Self.Table1.Fields[0].AsInteger :=StrToInt(BBList[0]);
for j:=1 to BBList.Count-1 do
begin
if BBList[j]<>'' then
begin
Self.Table1.Fields[j].AsFloat:=StrToFloat(BBList[j]);
end else
begin
Self.Table1.Fields[j].Value:=-9999;//处理数据为空的情况,这有别于数据为0的情况
end;
end;
Self.Table1.Post;
Self.ProgressBar1.Position :=i;
end;
Self.Table1.EnableControls;
Self.ProgressBar1.Position :=0;
llist.Free;
BBList.Free;
end;
end;
end;
end.