C
ccweifen
Unregistered / Unconfirmed
GUEST, unregistred user!
程序: 表字段比较多, 50多个字段 。
var
xlsapp,xlssheet:variant;
xlsfilename,xlsfiletitle,xlsfiledate:string;
i,j:integer;
Savedialog1 :TSaveDialog;
BookMark1:TBookMark ;
begin
SaveDialog1 :=TSaveDialog.create(Application);
SaveDialog1.Filter := 'Excel文件(*.xls)|*.XLS';
if savedialog1.Execute then
if savedialog1.FileName <>'' then
begin
xlsfilename := savedialog1.FileName ;
xlsfiletitle:='考勤汇总' ;
try
xlsapp:=createoleobject('excel.application');
xlssheet:=createoleobject('excel.sheet');
except
showmessage('本机没有安装Microsoft excel!');
savedialog1.Free ;
exit;
end;
Panel1.BringToFront ;
ProgressBar1.Min :=0 ;
ProgressBar1.Max :=ADOQuery.RecordCount ;
ProgressBar1.Position:=0 ;
Label7.Caption :='正在导出数据到'+xlsfilename ;
BookMark1:=ADOQuery.GetBookmark ;
ADOQuery.DisableControls ;
Application.ProcessMessages ;
Panel1.Refresh ;
Screen.Cursor :=crHourGlass ;
try
xlssheet:=xlsapp.workbooks.add;
ADOQuery.First;
// Label7.Caption :='正在'
for j := 0 to wwDBGrid1.Selected.Count-1 do
begin
xlsapp.Cells.item[1, j + 1].NumberFormatLocal :='@';
xlsapp.Cells.item[1, j + 1] := wwDBGrid1.Columns[J].DisplayLabel ;
xlsapp.Cells.item[1, j + 1].font.size := '10';
end;
for i := 2 to ADOQuery.RecordCount + 1 do
begin
for j := 0 to wwDBGrid1.Selected.Count-1 do
begin
case ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).DataType of
ftFloat, ftCurrency, ftBCD
: xlsapp.Cells.item[i, j + 1].NumberFormat := '0.00';
else xlsapp.Cells.item[i, j + 1].NumberFormatLocal :='@';
end;
xlsapp.Cells.item[i, j + 1] :=ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).AsString ;
xlsapp.Cells.item[i, j + 1].font.size := '10';
end;
ADOQuery.Next;
ProgressBar1.Position:=I-1 ;
Application.ProcessMessages ;
end;
xlsapp.Columns.AutoFit;
xlssheet.saveas(xlsfilename);
xlssheet.close;
xlsapp.quit;
xlsapp:=unassigned;
showmessage('数据已保存为: '+xlsfilename);
except
showmessage('数据转换出错!');
Panel1.SendToBack ;
xlssheet.close;
xlsapp.quit;
xlsapp:=unassigned;
end;
SaveDialog1.Free ;
Panel1.SendToBack ;
ADOQuery.EnableControls ;
ADOQuery.GotoBookmark(BookMark1) ;
ADOQuery.FreeBookmark(BookMark1) ;
end;
Screen.Cursor :=crDefault ;
我测试了导出20条记录 , 要用24秒钟,
一般汇总表记录在2500条左右,如果全导出的话估计要一个小时左右。岂不是要死人
如果去掉
case ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).DataType of
ftFloat, ftCurrency, ftBCD
: xlsapp.Cells.item[i, j + 1].NumberFormat := '0.00';
else xlsapp.Cells.item[i, j + 1].NumberFormatLocal :='@';
end ;
这格式化数据的代码,速度稍快点, 多一秒一条。
能不能一下子格式化EXCEL的一整列 ? 不用格式化每个单元格
var
xlsapp,xlssheet:variant;
xlsfilename,xlsfiletitle,xlsfiledate:string;
i,j:integer;
Savedialog1 :TSaveDialog;
BookMark1:TBookMark ;
begin
SaveDialog1 :=TSaveDialog.create(Application);
SaveDialog1.Filter := 'Excel文件(*.xls)|*.XLS';
if savedialog1.Execute then
if savedialog1.FileName <>'' then
begin
xlsfilename := savedialog1.FileName ;
xlsfiletitle:='考勤汇总' ;
try
xlsapp:=createoleobject('excel.application');
xlssheet:=createoleobject('excel.sheet');
except
showmessage('本机没有安装Microsoft excel!');
savedialog1.Free ;
exit;
end;
Panel1.BringToFront ;
ProgressBar1.Min :=0 ;
ProgressBar1.Max :=ADOQuery.RecordCount ;
ProgressBar1.Position:=0 ;
Label7.Caption :='正在导出数据到'+xlsfilename ;
BookMark1:=ADOQuery.GetBookmark ;
ADOQuery.DisableControls ;
Application.ProcessMessages ;
Panel1.Refresh ;
Screen.Cursor :=crHourGlass ;
try
xlssheet:=xlsapp.workbooks.add;
ADOQuery.First;
// Label7.Caption :='正在'
for j := 0 to wwDBGrid1.Selected.Count-1 do
begin
xlsapp.Cells.item[1, j + 1].NumberFormatLocal :='@';
xlsapp.Cells.item[1, j + 1] := wwDBGrid1.Columns[J].DisplayLabel ;
xlsapp.Cells.item[1, j + 1].font.size := '10';
end;
for i := 2 to ADOQuery.RecordCount + 1 do
begin
for j := 0 to wwDBGrid1.Selected.Count-1 do
begin
case ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).DataType of
ftFloat, ftCurrency, ftBCD
: xlsapp.Cells.item[i, j + 1].NumberFormat := '0.00';
else xlsapp.Cells.item[i, j + 1].NumberFormatLocal :='@';
end;
xlsapp.Cells.item[i, j + 1] :=ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).AsString ;
xlsapp.Cells.item[i, j + 1].font.size := '10';
end;
ADOQuery.Next;
ProgressBar1.Position:=I-1 ;
Application.ProcessMessages ;
end;
xlsapp.Columns.AutoFit;
xlssheet.saveas(xlsfilename);
xlssheet.close;
xlsapp.quit;
xlsapp:=unassigned;
showmessage('数据已保存为: '+xlsfilename);
except
showmessage('数据转换出错!');
Panel1.SendToBack ;
xlssheet.close;
xlsapp.quit;
xlsapp:=unassigned;
end;
SaveDialog1.Free ;
Panel1.SendToBack ;
ADOQuery.EnableControls ;
ADOQuery.GotoBookmark(BookMark1) ;
ADOQuery.FreeBookmark(BookMark1) ;
end;
Screen.Cursor :=crDefault ;
我测试了导出20条记录 , 要用24秒钟,
一般汇总表记录在2500条左右,如果全导出的话估计要一个小时左右。岂不是要死人
如果去掉
case ADOQuery.FieldByName(wwDBGRid1.Columns[J].FieldName).DataType of
ftFloat, ftCurrency, ftBCD
: xlsapp.Cells.item[i, j + 1].NumberFormat := '0.00';
else xlsapp.Cells.item[i, j + 1].NumberFormatLocal :='@';
end ;
这格式化数据的代码,速度稍快点, 多一秒一条。
能不能一下子格式化EXCEL的一整列 ? 不用格式化每个单元格