procedure TForm1.Button1Click(Sender: TObject);
var
  i,j,h,k:Integer;
  handle : HWND;
begin
  {Handle := FindWindow('XLMAIN', nil);    //判断Excel是否执行
  if handle <> 0 then
    showmessage('Excel is Running!')
  else
    showmessage('not Excel is Running!');
  }
  ExcelExistFlag := True;
  try
    EApp := CreateOleObject('Excel.Application.9') as _Application;  //Excel2000
  except
    ExcelExistFlag := False;
  end;
  if not ExcelExistFlag then
  try
    EApp := CreateOleObject('Excel.Application.8') as _Application;  //Excel97
    ExcelExistFlag := True;
  except
    ExcelExistFlag := False;
    ShowMessage('Excel调用失败!');
  end;  //EApp := CreateOleobject('Excel.Application');  if ExcelExistFlag then
  begin
    ADOQuery1.DisableControls;
    EApp.Visible:=true;
    //EApp.Caption := '还不行??';
    //EApp.Workbooks.Add;
    EApp.Workbooks.Add(xlWBatWorkSheet);
    EApp.Workbooks[1].WorkSheets[1].Name := Label1.Caption;
    ESheet := EApp.Workbooks[1].WorkSheets[Label1.Caption];
    ExcelCellRange := EApp.WorkBooks[1].WorkSheets[Label1.Caption].Columns;
    ADOQuery1.First;    for j:=0 to ADOQuery1.FieldCount-1  do
    begin
      //ESheet.Cells[1,j+1] :='';
      //ESheet.Cells[2,j+1] :='';
      //ESheet.Cells[3,j+1] :='';
      h:=Trunc(ADOQuery1.FieldCount/2);   //取整
      ESheet.Cells[1,h] :='北京有限公司';
      ESheet.Cells[2,h] :='用户信息表';
      ESheet.Cells[3,h] :='';
      ESheet.Cells[4,j+1] := DBGrid1.Columns[j].Title.Caption;
      //EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].ColumnWidth := DBGrid1.Columns[j].Width;
      EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].Borders.LineStyle := xlContinuous; //边框类型
      EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].Borders.Weight := xlMedium;        //边框粗细
      //ExcelCellRange.Columns[j+1].ColumnWidth :=DBGrid1.Columns[j].Field.Size;// DisplayWidth  ;
      ExcelCellRange.Columns[j+1].ColumnWidth := DBGrid1.Columns[j].Width/7;
      //ExcelCellRange.Columns.Item[j+1].Name:=DBGrid1.Columns[j].Title.Caption ;
      //ExcelCellRange.Characters.Font.Name := '宋体'; //字体
      //ExcelCellRange.Characters.Font.FontStyle := '加粗';
      //ExcelCellRange.Characters.Font.Size := 9;
      //ExcelCellRange.Characters.Font.Color := clBlue; //颜色
      //ExcelCellRange.Characters.Font.ColorIndex := xlAutomatic; //颜色
      //ExcelCellRange := EApp.WorkBooks[1].WorkSheets[1].Range['A1:D4'];
      //ExcelCellRange.Borders.LineStyle := xlDouble;
    end;
    for i:=1 to ADOQuery1.RecordCount  do
    begin
      for j:=1 to ADOQuery1.FieldCount  do
      begin
        ESheet.Cells[i+4,j] :=ADOQuery1.Fields[j-1].AsString;
        EApp.WorkBooks[1].WorkSheets[1].Cells[i+4,j].Borders.LineStyle := xlContinuous; //边框类型
        EApp.WorkBooks[1].WorkSheets[1].Cells[i+4,j].Borders.Weight := xlThin;          //边框粗细
        //ESheet.Cells[i+2,j].Characters.Font.Name := '宋体'; //字体
        //ESheet.Cells[i+2,j].Characters.Font.FontStyle := '';
        //ESheet.Cells[i+2,j].Characters.Font.Size := 9;      //大小
        //ESheet.Cells[i+2,j].Characters.Font.Color := clRed; //颜色
        //ESheet.Cells[i+1,j].Characters.Font.ColorIndex := xlAutomatic; //颜色
      end;
      ADOQuery1.next;
    end;
    k:=ADOQuery1.RecordCount+8;
    ESheet.Cells[k,1] :='★TikkyPeng★';
    ESheet.Cells[k,h] :=DateToStr(Date);   //'2001-03-14';
    ESheet.Cells[k,ADOQuery1.FieldCount] :='☆★☆★☆★☆'; //'TikkyPeng';
    //HandleRange;
    ADOQuery1.EnableControls;
    //EApp.Visible:=True;
  end
  else
    ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+'如果未安装,请先安装office');
    //ExcelCellRange.Characters.Font.Name := '宋体'; //字体
    //ExcelCellRange.Characters.Font.FontStyle := '';
    //ExcelCellRange.Characters.Font.Size := 9;
    {EApp.WorkBooks[1].WorkSheets[1].Range['A1'].Borders[xlBottom].LineStyle := xlContinuous;
    EApp.WorkBooks[1].WorkSheets[1].Range['A1'].Borders[xlBottom].Weight := xlHairline;
    EApp.WorkBooks[1].WorkSheets[1].Range['A2'].Borders[xlBottom].LineStyle := xlDash;  //虚线
    EApp.WorkBooks[1].WorkSheets[1].Range['A2'].Borders[xlBottom].Weight := xlThin;  //虚线
    EApp.WorkBooks[1].WorkSheets[1].Range['A3'].Borders.LineStyle := xlContinuous;
    EApp.WorkBooks[1].WorkSheets[1].Range['A3'].Borders.Weight := xlMedium;
    EApp.WorkBooks[1].WorkSheets[1].Range['A4'].Borders[xlBottom].LineStyle := xlDashDot;
    EApp.WorkBooks[1].WorkSheets[1].Range['A4'].Borders[xlBottom].Weight := xlThick;
    }
end;