我在书上看了一个例子,可是太复杂了,最好是有专门的控件来实现。不知谁知道如何解决?

解决方案 »

  1.   

    有很多人问过,你搜一下,贴些代码给你参考:
    acStore: TADOConnection;  {连接到Access}
    ...
    procedure TfrmEhlib.btnToExcelClick(Sender: TObject); 
    var
      fName: string;
    begin
      fName := ExtractFilePath(Application.ExeName) + 'store.xls';
      if FileExists(fName) then DeleteFile(fName);
      try
        acStore.Execute('select * into [Excel 8.0; Database=' + fName + '].[sheet1] from stable');
        MessageBox(self.Handle, '数据已成功导出为store.xls', '提示', mb_IconInformation + mb_Ok);
      except
        MessageBox(self.Handle, '数据导出失败!', '提示', mb_IconInformation + mb_Ok);
      end;
    end;procedure TfrmEhlib.btnFromExcelClick(Sender: TObject);
    var
      tName: TStrings;
    begin
      if not FileExists(ExtractFilePath(Application.ExeName) + 'store.xls') then Exit;
      tName := TStringList.Create;
      acStore.GetTableNames(tName, false);
      if tName.IndexOf('sExcel') >= 0 then
        acStore.Execute('drop table sExcel');
      acStore.Execute('select * into sExcel from [Excel 8.0; Database=' + ExtractFilePath(Application.ExeName)+ 'store.xls' + '].[sheet1$]');
      MessageBox(self.Handle, 'Excel已成功导入表sExcel', '提示', mb_IconInformation + mb_Ok);
      tName.Free;
    end;
      

  2.   

    用ado分别连接access和excel,然后想怎么办就随便了,其连接字符串如下:
    access:
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDB:Database Password=MyDbPassword;
    excel:
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"
      

  3.   

    procedure Tfrmoutput.ToolButton4Click(Sender: TObject);
    var
     i,j,m,n,h,g,d1,d2,s1,s2,b:integer;
     s,bh1,bh2,bh3,bh4,bh5:string;
     Excelid,xl: Variant;
    begin
      inherited;
      try
        Excelid:=CreateOleObject( 'Excel.Application' );
      except
        on Exception do raise exception.Create('无法创建Xls文件,请确认是否安装EXCEL')
      end;
      Excelid.Visible := True;
      Excelid.WorkBooks.Add;
      Excelid.Caption :='表aaaa';
      Excelid.worksheets[1].range['A1:L1'].Merge(True);
      Excelid.WorkSheets[1].Cells[1,1].Value :=QBaseInfo.fieldbyname('fcode').asstring;
      Excelid.worksheets[1].range['A1:J1'].HorizontalAlignment := $FFFFEFF4;
      Excelid.worksheets[1].range['A1:J1'].VerticalAlignment := $FFFFEFF4;
      Excelid.worksheets[1].Rows[1].RowHeight :=28;
      Excelid.worksheets[1].Columns[1].ColumnWidth :=1.75;
      Excelid.worksheets[1].Columns[2].ColumnWidth :=1.75;
      Excelid.worksheets[1].Columns[3].ColumnWidth :=10.75;
      Excelid.worksheets[1].Columns[4].ColumnWidth :=10.75;
      Excelid.worksheets[1].Columns[5].ColumnWidth :=6.65;
      Excelid.worksheets[1].Columns[6].ColumnWidth :=6.65;
      Excelid.worksheets[1].Columns[7].ColumnWidth :=6.65;
      
    //**************************************************************************************************
      Excelid.worksheets[1].range['B2:D2'].Merge(True);
      Excelid.WorkSheets[1].Cells[2,2].Value :='AAAAA’;
      Excelid.worksheets[1].Range['B2:D2'].Font.Size := 10;
      i:=3;
      Excelid.WorkSheets[1].Cells[i,2].Value := '序';
      Excelid.WorkSheets[1].Cells[i,3].Value := '名称';
      Excelid.WorkSheets[1].Cells[i,4].Value := '材料';
      Excelid.WorkSheets[1].Cells[i,5].Value := '单位';
      Excelid.WorkSheets[1].Cells[i,6].Value := '来源;
      
      Excelid.worksheets[1].Range['A1:L7'].Font.Name := '宋体';
      Excelid.worksheets[1].Range['A1:L1'].Font.Size := 14;
      Excelid.worksheets[1].range['A1:L1'].font.bold:=true;
      Excelid.worksheets[1].range['B3:L3'].font.bold:=true;
      Excelid.worksheets[1].Range['B3:L3'].Font.Size := 10;
      i:=4;
      Qa.First;
      while (not Qa.Eof)  do
        begin
              Excelid.WorkSheets[1].Cells[i,2].Value :=i-3;
              Excelid.WorkSheets[1].Cells[i,3].Value := Qa.fieldbyname('f1').asstring;
              Excelid.WorkSheets[1].Cells[i,4].Value := Qa.fieldbyname('f2').asstring;
              Excelid.WorkSheets[1].Cells[i,5].Value := floattostr(Qa.fieldbyname('f3').value
              Excelid.WorkSheets[1].Cells[i,6].Value := Qa.fieldbyname('f4').asstring;
            
              qa.Next;
              i:=i+1;
        end;end;
      

  4.   

    procedure TForm2.BitBtn3Click(Sender: TObject);
    var
      range:excelrange;
      i,j:integer;
      letter:char;
    begin
    if messagedlg('是否把差生内容导入到Excel?',mtconfirmation,[mbyes,mbno],0)=mryes then
      try
        try
        excelapplication1.Connect;
        except
        showmessage('您的机器没有安装Excel!');
        abort;
        end;
        excelapplication1.Visible[0]:=true;
        excelapplication1.Caption:='查询分析内容';
        excelapplication1.Workbooks.Add(NUll,0);    i:=65+datasource1.DataSet.FieldCount-1;
        letter:=char(i);    range:=excelapplication1.Range['A1',letter+'1'];    for i:=0 to datasource1.DataSet.FieldCount-1 do
          begin
          range.Value:=datasource1.DataSet.Fields[i].DisplayLabel;
          range:=range.Next;
          end;    datasource1.DataSet.First;    for i:=1 to datasource1.DataSet.RecordCount do begin
          range:=excelapplication1.Range['A'+inttostr(i+1),letter+inttostr(i+1)];
          for j:=0 to datasource1.DataSet.FieldCount-1 do begin
            if not datasource1.DataSet.Fields[j].IsBlob then
            range.Value:=datasource1.DataSet.Fields[j].AsString
            else range.Value:='不支持图片显示';
            range:=range.Next;
            end;
          datasource1.DataSet.Next;
          end;  except
      excelapplication1.Disconnect;
      end;
    end;
      

  5.   

    以上是把dbgrid得内容,也就是数据集得内容导入excel阿,里面使用delphi 7得server面板上面得ExcelApplication1组件,进行操纵,这个组件通过Range属性进行excel得表得内容得定位,定位之后就可以导入excel了阿,你看看我得程序,应该可以理解得!!
      

  6.   

    关注!excel文件如何导入access数据库的,并且在dbgrid中显示!