Server不会慢,代码如下:
procedure TfrmExecl.btnExportClick(Sender: TObject);
var
wkBook: _Workbook;
wkSheet: _Worksheet;
LCID,Line,Col,i,j: integer;
FileName: TFileName;
Datas,HeadLine: Variant;
begin
SaveDialog1.Execute;
FileName := '';
FileName := SaveDialog1.FileName;
if FileName = '' then Abort;
Line := ADODataSet1.Recordset.RecordCount;
Col := ADODataSet1.Fields.Count;
Datas := VarArrayCreate([1,Line,1,Col],varVariant);
HeadLine := VarArrayCreate([1,Col],varVAriant);
ExcelApplication1.Connect;
LCID := GetUserDefaultLCID();
wkBook := ExcelApplication1.Workbooks.Add(EmptyParam, LCID);
wkSheet := wkBook.Sheets[1] as _WorkSheet;
ADODataSet1.DisableControls;
for i := 0 to Col - 1 do
HeadLine[i+1] := ADODataSet1.Fields.FieldName;
ADODataSet1.First;
for j := 0 to Line - 1 do
begin
for i := 0 to Col - 1 do
Datas[j+1,i+1] := ADODataSet1.Fields.Value;
ADODataSet1.Next;
end;
with wkSheet do
begin
Range[cells.Item[1,1],cells.Item[1,Col]].Value := HeadLine;
Range[cells.Item[2,1],cells.Item[Line+1,Col]].Value := Datas;
end;
ADODataSet1.EnableControls;
wkBook.Close(True, FileName, EmptyParam, LCID);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
MessageBox(self.handle,pchar('导出Excel文件:' + FileName + '完毕!'),
'导出成功',MB_OK+MB_ICONINFORMATION+MB_TOPMOST)
end;