王宏健的专栏 ---- 健康快乐

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

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

uses ADODB,excel97,adoint;

function TForm1.ExportToExcel: Boolean;
var
  xlApp,xlBook,xlSheet,xlQuery: Variant;
  adoConnection,adoRecordset: Variant;
begin
  adoConnection := CreateOleObject('ADODB.Connection');
  adoRecordset := CreateOleObject('ADODB.Recordset');
  adoConnection.Open('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Tree.mdb;Persist Security Info=False');
  adoRecordset.CursorLocation := adUseClient;
  adoRecordset.Open('SELECT * FROM tree',adoConnection,1,3);

  try
    xlApp := CreateOleObject('Excel.Application');
    xlBook := xlApp.Workbooks.Add;
    xlSheet := xlBook.Worksheets['sheet1'];
   
    //设置这一列为 文本列 ,让 "00123" 正确显示, 而不是自动转换为"123"
    xlSheet.Columns['C:C'].NumberFormatLocal := '@';

    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;

    xlBook.SaveAs('d:\fromD.xls',xlNormal,'','',False,False);

  finally
    if not VarIsEmpty(XLApp) then begin
      XLApp.displayAlerts:=false;
      XLApp.ScreenUpdating:=true;
      XLApp.quit;
    end;
  end;
end;

posted on 2004-11-29 17:02  LuckyJan  阅读(1989)  评论(1)    收藏  举报