如何把interbase或paradox数据表导出为Excel?(100分)

  • 主题发起人 主题发起人 jonva
  • 开始时间 开始时间
J

jonva

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把interbase或paradox数据表导出为Excel?如何导入Excel数据内容?
 
查查以前的帖子,讨论很多了。或者去深度历险上找一个空件。
 
最快,最简单的办法——存成逗号分隔的CSV格式。
procedure ExportDataSetToCSVFile(const aDataSet:TDataSet;
const aFileName:string);
procedure ImportFromCSVFile(const AFileName:String;
const ADataSet:TDataSet);
procedure ImportFromCSVFile2(const AFileName:String;
const ADataSet:TDataSet);
function GetLeftWord(var ASentence:string; ADelimiter:char):string;

function GetLeftWordCSV(var ASentence:string):string;
begin
Result:='';
ASentence:=Trim(ASentence);// remove spaces and strange chars in CSV
if Length(ASentence)=0 then exit;
if ASentence[1]='"' then begin
Delete(ASentence,1,1);
Result:=GetLeftWord(ASentence,'"');
GetLeftWord(ASentence,',');//get rid of comma
end else Result:=GetLeftWord(ASentence,',');
end;

function GetLeftWord(var ASentence:string; ADelimiter:char):string;
var i:integer;
begin
Result := '';
i := Pos(ADelimiter,ASentence);
if i = 0 then begin
Result := Trim(ASentence);
ASentence := '';
end else begin
Result:=trim(Copy(ASentence,1,i-1));
Delete(ASentence,1,i);
end;
end;


procedure ExportDataSetToCSVFile(const aDataSet:TDataSet;
const aFileName:string);
var aTextFile:TextFile;
i:integer;
aValue,LineStr:string;
DataSetActive:boolean;
begin
DataSetActive := aDataSet.Active;
if not aDataSet.Active then aDataSet.Open;
aDataSet.DisableControls;
try
aDataSet.FieldDefs.Update;
if aDataSet.RecordCount=0 then exit;
try
AssignFile(aTextFile,aFileName);
Rewrite(aTextFile);
LineStr := '';
for i:=0 to aDataSet.FieldCount-1 do begin
aValue := aDataSet.Fields.DisplayName ;
if Pos(',',aValue) > 0 then aValue:='"' + aValue + '"';
LineStr := LineStr + aValue + ',';
end;
Delete(LineStr,length(LineStr),1);// remove extra ','
Writeln(aTextFile,LineStr);
aDataSet.First;
while not aDataSet.EOF do begin
LineStr := '';
for i := 0 to aDataSet.FieldCount-1 do begin
if aDataSet.Fields.IsBlob then begin
if aDataSet.Fields.DataType=ftMemo then
aValue :=StringReplace(aDataSet.Fields.AsString,#13+#10,'#',[rfReplaceAll]);
end else begin
aValue := aDataSet.Fields.AsString;
end;
if Pos(',',aValue) > 0 then aValue := '"' + aValue + '"';
LineStr := LineStr+aValue+',';
end;
Delete(LineStr,length(LineStr),1);// remove extra ','
Writeln(aTextFile,LineStr);
aDataSet.Next;
end;// while
finally
try
Flush(aTextFile);
CloseFile(aTextFile);
except
end;
end;
finally
aDataSet.EnableControls;
aDataset.Active := DataSetActive;
end;
end;

procedure ImportFromCSVFile(const AFileName:String;
const ADataSet:TDataSet);
const cMaxFields=1023;
var
aTextFile:TextFile;
i,j,HeaderCount:integer;
FldPtr:array[0..cMaxFields] of integer;
aColValue,aLineString,aValue:string;
HasCommonField,DataSetActive:boolean;
begin
if not FileExists(AFileName) then exit;
AssignFile(aTextFile,AFileName);
Reset(aTextFile);
try
{ first row must be headers }
Readln(aTextFile, ALineString);
HasCommonField:=False;
HeaderCount:=0;
DataSetActive:=ADataSet.Active;// save the status of the dataset
if not ADataset.Active then ADataset.Open; // open if closed
ADataSet.FieldDefs.Update;
while (length(ALineString) > 0) and (HeaderCount < cMaxFields) do begin
aColValue := GetLeftWordCSV(ALineString);
for i:=0 to ADataSet.FieldCount-1 do
if Trim(ADataSet.Fields.DisplayName)=trim(aColValue) then
begin
j :=i;
FldPtr[HeaderCount] := j;
if j > -1 then HasCommonField:=True;
inc(HeaderCount);
break;
end;
end;
if not HasCommonField then exit; // not a single field found on list
ADataSet.DisableControls;
try
while not EOF(aTextFile) do begin
Readln(aTextFile,ALineString);
j := -1;
ADataSet.Append;
while (length(ALineString) > 0) and (j < HeaderCount-1) do begin
aColValue := GetLeftWordCSV(ALineString);
inc(j);
if FldPtr[j] = -1 then continue; // skip unmatched columns
try
if Trim(aColValue)<>'' then
begin
if ADataSet.Fields[FldPtr[j]].DataType=ftMemo then
ADataSet.Fields[FldPtr[j]].AsString :=StringReplace(aColValue,'#',#13+#10,[rfReplaceAll])
else
ADataSet.Fields[FldPtr[j]].AsString := aColValue;
end;
except
// simply ignore all data conversion errors
// or handle it here as you wish
end;
end; // while (length(ALineString)...
ADataSet.Post;
end; // while not EOF(aTextFile)
finally
ADataSet.EnableControls;
ADataSet.Active := DataSetActive; // restore dataset status
end;
finally
CloseFile(aTextFile);
end;
end; // procedure

要存成XLS格式,一般就只能通过OLE。这个如楼上所说,论坛上例子很多
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
816
SUNSTONE的Delphi笔记
S
后退
顶部