procedure TUPLocal_F.Button2Click(Sender: TObject); var i:integer; begin with LDQ do //源数据 begin Close; SQL.clear; SQL.Add('select Top 0 A1,A2'); SQL.Add('from T1); open; end; with DQ1 do //目的数据集 begin Close; SQL.Clear; SQL.Add('select Top 0 A1,A2'); SQL.Add('from T1'); prepared:=true; open; LDQ.First; while Not LDQ.Eof do begin append; for i:=0 to FieldCount-1 do Fields[i].Value:=LDQ.Fields[i].Value; Try Post; except end; end; end;end;
我这里有个导入Excell的,参照着写吧: procedure TFormGzqkHZ.DataToExcel(aPath: string); var MySQL,ConnStr: string; begin ConnStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=' + aPath + ';Persist Security Info=False'; MySQL := 'SELECT * INTO [SHEET1] FROM TBLGZFFQKLS IN [ODBC]' + ' [ODBC;Driver=SQL Server;UID=sa;PWD=;Server='+ frmMain.IpCode +';DataBase=SalaryAuidit;]'; try ADOConnExcel.Connected := False; ADOConnExcel.ConnectionString := ConnStr; ADOConnExcel.Connected := True; ADOConnExcel.Execute(MySQL); Application.MessageBox('导出EXCEL成功','信息提示!',mb_ok+mb_iconinformation); ADOConnExcel.Connected := False; except Application.MessageBox('导出EXCEL失败','错误!',mb_ok+mb_iconStop); ADOConnExcel.Connected := False; end; end;procedure TFormGzqkHZ.SpeedBPathClick(Sender: TObject); begin if OpenDiaEX.Execute then begin EditPath.Text := OpenDiaEX.FileName; end; end;procedure TFormGzqkHZ.BitBExcellClick(Sender: TObject); var MyPath: string; begin if EditPath.Text = '' then begin Application.MessageBox('请重新输出并选择导出的文件路径','提示',mb_ok+mb_iconstop); Abort; end else begin MyPath := EditPath.Text; if ExtractFileExt(MyPath) = '' then begin MyPath := MyPath + '.xls'; end; end; if not DirectoryExists(ExtractFilePath(MyPath)) then ForceDirectories(ExtractFilePath(MyPath)); if FileExists(MyPath) then begin if Application.MessageBox('文件已存在,是否覆盖?','提示!',mb_yesno+mb_iconquestion) = idyes then begin DeleteFile(MyPath); end else begin Exit; end; end; DataToExcel(MyPath); PanelPath.Visible := False; end;
var
i:integer;
begin
with LDQ do //源数据
begin
Close;
SQL.clear;
SQL.Add('select Top 0 A1,A2');
SQL.Add('from T1);
open;
end; with DQ1 do //目的数据集
begin
Close;
SQL.Clear;
SQL.Add('select Top 0 A1,A2');
SQL.Add('from T1');
prepared:=true;
open; LDQ.First; while Not LDQ.Eof do
begin
append;
for i:=0 to FieldCount-1 do
Fields[i].Value:=LDQ.Fields[i].Value;
Try
Post;
except
end; end;
end;end;
procedure TFormGzqkHZ.DataToExcel(aPath: string);
var
MySQL,ConnStr: string;
begin
ConnStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=' + aPath + ';Persist Security Info=False';
MySQL := 'SELECT * INTO [SHEET1] FROM TBLGZFFQKLS IN [ODBC]' + ' [ODBC;Driver=SQL Server;UID=sa;PWD=;Server='+ frmMain.IpCode +';DataBase=SalaryAuidit;]';
try
ADOConnExcel.Connected := False;
ADOConnExcel.ConnectionString := ConnStr;
ADOConnExcel.Connected := True;
ADOConnExcel.Execute(MySQL);
Application.MessageBox('导出EXCEL成功','信息提示!',mb_ok+mb_iconinformation);
ADOConnExcel.Connected := False;
except
Application.MessageBox('导出EXCEL失败','错误!',mb_ok+mb_iconStop);
ADOConnExcel.Connected := False;
end;
end;procedure TFormGzqkHZ.SpeedBPathClick(Sender: TObject);
begin
if OpenDiaEX.Execute then
begin
EditPath.Text := OpenDiaEX.FileName;
end;
end;procedure TFormGzqkHZ.BitBExcellClick(Sender: TObject);
var
MyPath: string;
begin
if EditPath.Text = '' then
begin
Application.MessageBox('请重新输出并选择导出的文件路径','提示',mb_ok+mb_iconstop);
Abort;
end
else
begin
MyPath := EditPath.Text;
if ExtractFileExt(MyPath) = '' then
begin
MyPath := MyPath + '.xls';
end;
end;
if not DirectoryExists(ExtractFilePath(MyPath)) then
ForceDirectories(ExtractFilePath(MyPath));
if FileExists(MyPath) then
begin
if Application.MessageBox('文件已存在,是否覆盖?','提示!',mb_yesno+mb_iconquestion) = idyes then
begin
DeleteFile(MyPath);
end
else
begin
Exit;
end;
end;
DataToExcel(MyPath);
PanelPath.Visible := False;
end;
这个问题应该在数据库版问的,呵呵,祝你好运