做点好事,给你一个
procedure BuildExcelFile(CDSData:TClientDataSet;psTitle,psFileName: string);
var
Row,Col:Integer;
lsDir:string;
begin
lsDir:=ExtractFilePath(psFileName);
if (lsDir<>'') and not DirectoryExists(lsDir) then
ForceDirectories(lsDir);
XLSRW.Filename:=psFileName;
if FileExists(psFileName) then
XLSRW.Read;
for Row:=1 to 1000000 do
begin
if (XLSRW.Sheet[0].AsString[0,Row]='') and (XLSRW.Sheet[0].AsString[1,Row]='')
and (XLSRW.Sheet[0].AsString[2,Row]='') and (XLSRW.Sheet[0].AsString[3,Row]='') then
Break;
end;
if Row=1 then
begin
XLSRW.Sheet[0].MergedCells.Add(0,0,FTitList.Count-1,0);
XLSRW.Sheet[0].AsString[0,0]:=psTitle;
XLSRW.Sheet[0].ApplyFormat.Alignment(0,0,0,0,chaCenter,cvaCenter);
end;
for Col:=0 to FTitList.Count-1 do
XLSRW.Sheet[0].AsString[Col,Row]:=FTitList[Col];
Row:=Row+1;
CDSData.DisableControls;
Screen.Cursor:=crHourGlass;
try
CDSData.First;
while not CDSData.Eof do
begin
for Col:=0 to FFieldList.Count-1 do
begin
if CDSData.FieldByName(FFieldList[Col]).IsNull then
Continue;
case CDSData.FieldByName(FFieldList[Col]).DataType of
ftSmallint, ftInteger, ftWord:
XLSRW.Sheet[0].AsInteger[Col,Row]:=CDSData.FieldByName(FFieldList[Col]).AsInteger;
ftFloat, ftCurrency, ftBCD:
XLSRW.Sheet[0].AsFloat[Col,Row]:=CDSData.FieldByName(FFieldList[Col]).AsFloat;
ftDate, ftTime, ftDateTime:
XLSRW.Sheet[0].AsDateTime[Col,Row]:=CDSData.FieldByName(FFieldList[Col]).AsDateTime;
else
if SameText(CDSData.FieldByName(FFieldList[Col]).AsString,'true') then
XLSRW.Sheet[0].AsString[Col,Row]:='是'
else if SameText(CDSData.FieldByName(FFieldList[Col]).AsString,'false') then
XLSRW.Sheet[0].AsString[Col,Row]:=''
else
XLSRW.Sheet[0].AsString[Col,Row]:=CDSData.FieldByName(FFieldList[Col]).AsString;
end;
end;
Row:=Row+1;
CDSData.Next;
end;
XLSRW.Write;
Shellexecute(Application.Handle, 'open',pchar(psFileName), nil, '', SW_SHOWDEFAULT) ;
finally
CDSData.EnableControls;
Screen.Cursor:=crDefault;
end;
end;