如何将access里头的查询导出成excel
最好有个界面让选择access文件.然后存文件也有个选择的路径的.谢谢先

解决方案 »

  1.   

    1 ACCESS 导入2 导出EXCEL
    那么,1 你打算导入到哪2 从STRINGGRID导出EXCEL 偶有现成语句,但前提是1是什么
      

  2.   

    就是现成的access文件.
    比如里头有两个表t1  t2我现在想导出
    select t1.* from t1,t2 where t1.id=t2.id想导出这个查询的结果至一个excel文件.
      

  3.   

    procedure TForm_vessel_ie.Btn_outClick(Sender: TObject);
    var
      strCurrentDir: string;
      strfilename: string;
      i, j: integer;
      eclApp, WorkBook: variant;
    begin
      strCurrentDir := GetCurrentDir();
      try
        eclApp := CreateOleObject('Excel.Application');
        WorkBook := CreateOleObject('Excel.Sheet');
      except
        Application.MessageBox('你的机器未装MS EXCEL.', '操作提示-警告', MB_ICONWARNING);
        exit;
      end;  try
        if savedialog1.Execute then
        begin
          strfilename := savedialog1.FileName;
        end
        else
          exit;    ProgressBar1.Visible := true;
        ProgressBar1.Max := sg_List.RowCount;
        lbl_Message.Font.Color := clgreen;
        lbl_Message.Caption := '开始导出.';
        workBook := eclApp.workBooks.add;
        eclApp.cells[1, 13] := 'title';
        
        for i := 0 to sg_List.RowCount - 1 do
        begin
          for j := 1 to sg_List.ColCount - 1 do
          begin
             eclApp.cells[i + 3, j] := sg_List.Cells[j, i];
          end;
          self.Repaint;
        end;
        workBook.saveas(strfilename);
        workBook.close;
        eclApp.quit;
        eclApp := unassigned;
      except
        Application.MessageBox('不能操作该文件.可能是该文件已被其他程序打开,或文件错误.', '操作提示-警告', MB_ICONWARNING);
        workBook.close;
        eclApp.quit;
        eclApp := unassigned;
      end;
        SetCurrentDir(strCurrentDir);
    end;
    这个是从TSTRINGGRID导出到EXCEL的代码,你可以自己改
      

  4.   

    ProgressBar1.Visible := true;
        ProgressBar1.Max := sg_List.RowCount;
        lbl_Message.Font.Color := clgreen;
        lbl_Message.Caption := '开始导出.';无视这段话
      

  5.   

    还有个更简单的办法,楼上要创建EXCEL对,还可以这样var tmpsource,tmpdest:tadoquery;
        i:integer;
        reccount:string;
    begin
      if not fileexists(edt_file.Text) then
        begin
        application.MessageBox('文件不存在,请选择要导入的文件!','提示',mb_ok+mb_iconinformation);
        exit;
        end;
      dm_main.adocnmain.BeginTrans;
      tmpsource:=tadoquery.Create(nil);
      tmpsource.ConnectionString:='provider=microsoft.jet.oledb.4.0;data source='+edt_file.Text+
                                    ';extended properties =excel 8.0;persist security info=false';
      tmpdest:=tadoquery.Create(nil);
      tmpdest.ConnectionString:=dm.adocnmain.ConnectionString;
        gauge1.MaxValue :=tmpsource.RecordCount;
        reccount:=inttostr(tmpsource.RecordCount);
        try
          for i:=0 to tmpsource.RecordCount-1 do
          begin
          with tmpdest do
            begin
            sql.Clear;
            sql.Add('insert into T_stu (stuid,stuname,stusex,sturace,stugrade,stuclass,stubg,workdate,borndate,stuaddress,stumail,logpsw)');
            sql.Add('values (:v1,:v2,:v3,:v4,:v5,:v6,:v7,:v8,:v9,:v10,:v11,:v12)');
            parameters.ParamByName('v1').Value :=tmpsource.FieldByName('StuID').AsString;
            parameters.ParamByName('v2').Value:=tmpsource.FieldByName('stuname').AsString;
            parameters.ParamByName('v3').Value:=tmpsource.FieldByName('stusex').AsString;
            parameters.ParamByName('v4').Value:=tmpsource.FieldByName('sturace').AsString;
            parameters.ParamByName('v5').Value:=tmpsource.FieldByName('stugrade').AsString;
            parameters.ParamByName('v6').Value:=tmpsource.FieldByName('stuclass').AsString;
            parameters.ParamByName('v7').Value:=tmpsource.FieldByName('stubg').AsString;
            parameters.ParamByName('v8').Value:=tmpsource.FieldByName('workdate').AsDateTime;
            parameters.ParamByName('v9').Value:=tmpsource.FieldByName('borndate').AsDateTime;
            parameters.ParamByName('v10').Value:=tmpsource.FieldByName('stuaddress').AsString;
            parameters.ParamByName('v11').Value:=tmpsource.FieldByName('stumail').AsString;
            parameters.ParamByName('v12').Value:=IniPsw;
            execsql;
            end;
          tmpsource.Next;
          gauge1.AddProgress(gauge1.MaxValue div tmpsource.RecordCount);
          end;
          dm.adocnmain.CommitTrans;
          application.MessageBox('数据导入成功!','提示',mb_ok+mb_iconinformation);
          close;
        except
          on e:exception do
          begin
            dm.adocnmain.RollbackTrans;
            application.MessageBox('数据导入失败!''警告',mb_ok+mb_iconwarning);
          end;
        end;
      finally
        tmpsource.Free;
        tmpdest.Free;
      end;
      

  6.   

    这个也是个办法啦不过把EXCEL当数据源了哦如果是要在EXCEL 里指定什么地方放什么数据就不行了
      

  7.   

    nb95463034 你好,用了你的代码.出现了几个问题
    CreateOleObject 这个报错了,不晓得是什么,是不是前面得引用什么单元?
    sg_List 这个是用了ListBox组件吗
      

  8.   

    1 你F1 CreateOleObject 就会发现需要引用的单元名字2 SG_LIST 是STRINGGIRD,存放要导成EXCEL的数据
      其实就是把SG_LIST中的CELLS[x,y]写EXCEL里对应单元格