procedure genxls(sdata: Tdataset; filename: Tfilename); var I, row, fieldnos, columnlen, recount: integer; xlapp, sheet: variant; fieldname: string; begin // try try xlapp := createoleobject('excel.application'); except showmessage('not found excel in your system, so can not create file!'); exit; end; xlapp.visible := false; xlapp.workbooks.add(-4167); sheet := xlapp.workbooks[1].worksheets[1]; fieldnos := sdata.FieldCount; sdata.first; recount := sdata.RecordCount; if recount = 0 then recount := 1; for I := 1 to fieldnos do begin columnlen := sdata.Fields.Fields[i - 1].Size; fieldname := uppercase(sdata.Fields.Fields[i - 1].displaylabel); sheet.cells[1, i] := fieldname; if columnlen = 0 then columnlen := 10; if columnlen < length(fieldname) then begin columnlen := length(fieldname); end; if columnlen>30 then columnlen:=20; sheet.columns.columns[i].columnwidth := columnlen; if sdata.Fields.Fields[i - 1].DataType = ftString then xlapp.Range[sheet.cells[2, i], sheet.cells[recount + 1, i]].numberformat := '@'; //此出是设置该列为文本格式,防止字符‘001’变成‘1’ end; for row := 2 to recount + 1 do begin for i := 1 to fieldnos do begin sheet.cells[row, i] := showGB(trim(sdata.Fields.Fields[i - 1].asstring)); //繁体转简体 end; sdata.Next; end; sheet.cells[1, 1].select; if not varisempty(xlapp) then xlapp.displayalerts := false; sheet.saveas(filename); showmsg('玂Θ', '', 1);//自己做的信息提示窗口以显示繁体 finally xlapp.quit; end;end;
var I, row, fieldnos, columnlen, recount: integer;
xlapp, sheet: variant;
fieldname: string;
begin
//
try
try
xlapp := createoleobject('excel.application');
except
showmessage('not found excel in your system, so can not create file!');
exit;
end;
xlapp.visible := false;
xlapp.workbooks.add(-4167);
sheet := xlapp.workbooks[1].worksheets[1];
fieldnos := sdata.FieldCount;
sdata.first;
recount := sdata.RecordCount;
if recount = 0 then recount := 1;
for I := 1 to fieldnos do
begin
columnlen := sdata.Fields.Fields[i - 1].Size;
fieldname := uppercase(sdata.Fields.Fields[i - 1].displaylabel);
sheet.cells[1, i] := fieldname;
if columnlen = 0 then columnlen := 10;
if columnlen < length(fieldname) then
begin
columnlen := length(fieldname);
end;
if columnlen>30 then columnlen:=20;
sheet.columns.columns[i].columnwidth := columnlen;
if sdata.Fields.Fields[i - 1].DataType = ftString then
xlapp.Range[sheet.cells[2, i], sheet.cells[recount + 1, i]].numberformat := '@'; //此出是设置该列为文本格式,防止字符‘001’变成‘1’
end;
for row := 2 to recount + 1 do begin
for i := 1 to fieldnos do
begin
sheet.cells[row, i] := showGB(trim(sdata.Fields.Fields[i - 1].asstring)); //繁体转简体
end;
sdata.Next;
end;
sheet.cells[1, 1].select;
if not varisempty(xlapp) then
xlapp.displayalerts := false;
sheet.saveas(filename);
showmsg('玂Θ', '', 1);//自己做的信息提示窗口以显示繁体
finally
xlapp.quit;
end;end;