(Delphi)Excel的快速导入

类别:Delphi 点击:0 评论:0 推荐:

//怎样可以提高EXCEL的导出速度?

function TForm1.ExportToExcel: Boolean;
var
  xlApp,xlBook,xlSheet,xlQuery: Variant;
  adoConnection,adoRecordset: Variant;
begin
  adoConnection := CreateOleObject('ADODB.Connection');
  adoRecordset := CreateOleObject('ADODB.Recordset');
  adoConnection.Open(ADOConnectionString);
  adoRecordset.CursorLocation := adUseClient;
  adoRecordset.Open('SELECT * FROM tableName',adoConnection,1,3);

  xlApp := CreateOleObject('Excel.Application');
  xlBook := xlApp.Workbooks.Add;
  xlSheet := xlBook.Worksheets['sheet1'];
  xlApp.Visible := True;

  //把查询结果导入EXCEL数据

  xlQuery := xlSheet.QueryTables.Add(adoRecordset,xlSheet.Range['A1']);  //关键是这一句
  xlQuery.FieldNames := True;
  xlQuery.RowNumbers := False;
  xlQuery.FillAdjacentFormulas := False;
  xlQuery.PreserveFormatting := True;
  xlQuery.RefreshOnFileOpen := False;
  xlQuery.BackgroundQuery := True;
  //xlQuery.RefreshStyle := xlInsertDeleteCells;
  xlQuery.SavePassword := True;
  xlQuery.SaveData := True;
  xlQuery.AdjustColumnWidth := True;
  xlQuery.RefreshPeriod := 0;
  xlQuery.PreserveColumnInfo := True;
  xlQuery.FieldNames := True;
  xlQuery.Refresh;

本文地址:http://com.8s8s.com/it/it5358.htm