您的位置:首页 > 数据库

把数据库表里的信息 导入Execl表 程序截图

2010-08-11 17:18 218 查看
uses

   Excel2000, Math, OleServer

添加 ToolBar1, Gauge1,ImageList1,ExcelWorkbook1,ExcelWorksheet1,ADOConnection1,ADOQuery1,DataSource1 ,Memo1,DBGrid1,StatusBar1



  SQLStr := '';
SQLStr :=Memo1.Lines.GetText;

ADOQuery1.Close;
ADOQuery1.Sql.Clear;
ADOQuery1.SqL.Add(SQLStr);
ADOQuery1.Open;
end;

procedure TForm1.ToolButton7Click(Sender: TObject);
var
ExcelApp1:TExcelApplication;  //uses Excel2000
Row,i:Integer;
AFileName : string;
begin
AFileName := 'D:\a.xlsx';
ExcelApp1:=TExcelApplication.Create(nil);
try
ExcelApp1.Connect;//和Execl连接如果没有Excel程序可能要出错
ExcelApp1.Workbooks.Add(Null,0);//为Excel添加工作薄
ExcelWorkbook1.ConnectTo(ExcelApp1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);

if DBGrid1.DataSource.DataSet.RecordCount=0 then Exit;
row:=2;
for i:=1 to ADOQuery1.Fields.Count do
begin
ExcelWorkSheet1.Cells.Item[1,i]:=ADOQuery1.Fields[i-1].FieldName ;
end;
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
Application.ProcessMessages;
for i:=1 to ADOQuery1.Fields.Count do
begin //把查询结果写入到电子表格中
excelworksheet1.Cells.Item[row,i].numberformatlocal:='@';
ExcelWorkSheet1.Cells.Item[row,i]:=ADOQuery1.Fields[i-1].Value;
end;
row:=row+1;
Gauge1.Progress:=Ceil(100*(row-2)/(ADOQuery1.RecordCount));
if Gauge1.Progress=100 then StatusBar1.Panels[1].Text:='导出完成,已保存至'+AFileName;          //AFileName是导出文件存放的目录位置。
ADOQuery1.Next;
end;
finally
ExcelWorkBook1.SaveCopyAs(AFileName);
ExcelWorkSheet1.Disconnect;
ExcelWorkBook1.Close(False);
ExcelApp1.Disconnect;
ExcelApp1.Quit;
ExcelApp1.Free;
Gauge1.Visible:=False;
end;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐