参考《delphi4编程技术内幕》,里面有比较详细的讲解。
解决方案 »
- 把image里的图片(jpeg) 里的图片保存到access2000 ole字段 保存不上呀!!
- 求一方案.一个系统怎样连接两个数据库?
- 关于 DLL 编程
- 初学delphi,买了DELPHI8,不过学习是用DELPHI7的书,有点问题不明?
- 还急!!求助delphi高手!!送分,送QQ,共享我机器里的好东西!!谢谢!!
- 帮忙推荐一些关于windows消息机制的资料<打赏100文啦>
- 如何使一个将ClientDataSet中的值赋给另外一个空的ClientDataSet
- avicap32.dll 判断摄像头状态
- 抢分问题:怎样才能查询出表中某字段的最大值,并把它加1 后 赋给edit.text?
- 高手请进!关于捕获程序运行时错误的问题。
- 菜鸟笨笨地问。
- 问题串烧啊,现考现卖,答对有奖啊!
ADODataSet和BDEDataSet访问数据表相类似。对于ADO来说,要说明数据源然后利用数据感
知(data-aware)控件将数据显示在应用程序中。首先要设置的是CommandType,它用来告
诉ADO引擎准备调用什么样的命令。3.设置ConnectionString属性
ConnectionString属性告诉ADO如何联接数据源。点击省略号会显示ConnectionString对话
框。我们要自己创建ConnectionString所以选择Use Connection String,然后点击Build
按钮激活Data Link Properties对话框。首先要做的是选择我们要用的数据提供者(Data
Provider)以取得数据,以为要访问的对象是Excel,所以我们选择Microsoft JET OLEDB
4.0 Provider,点击NEXT。下一步是要选择联接的具体对象,通过Browse按钮,选择任意
一个Excel文件或者使用我们提供的样例文件Forecast.xls。在测试ConnectionString之前,
我们还要手动的添加Extended Property,点击OK返回到ConnectionString对话框然后输入
’Extended Properties=Excel 8.0;’别忘了每一个ConnectionString实体都需要用分号
隔离。这是要对ADO声明我们需要Excel支持并使类库关联到Excel 8.0上。点击Build按钮,
然后点击TEST,如果返回正确的结果,这一步的工作就完成了。4.设置CommandText属性
该属性变成下来表单,允许你选择数据源中定义的表,选择a$。5.在Form中放置Datasource(Data Access)组件,和DBGrid(Data Controls)。
我们需要将ADODataset连接到网格上显示数据。将DBGrid的DataSource属性设置为
DataSource1,DataSource的ADODataSet属性设置为ADODataSet1。6.将ADODataSet的Active属性设为True
通过激活ADODataSet,我们可以在设计模式下测试查询的运行状态。来自于EXCEL的数据
将显示在网格中。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, DB, DBTables,ComObj, StdCtrls;type
TForm1 = class(TForm)
DataSource1: TDataSource;
Table1: TTable;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
procedure WriteToExcel(adataset: TDataSet; selrows: TBookList); { Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
myexcel:variant;
workbook:olevariant;
worksheet:olevariant;
i,j:integer;
begin
try
myexcel:=createoleobject('excel.application');
myexcel.application.workbooks.add;
myexcel.caption:='将数据导入到EXCEL表中';
myexcel.application.visible:=true;
workbook:=myexcel.application.workbooks[1];
worksheet:=workbook.worksheets.item[1];
except
showmessage('EXCEL不存在!');
end;
i:=0;
table1.first;
while not table1.eof do
begin
inc(i);
for j:=0 to table1.fieldcount-1 do
worksheet.cells[i,j+1]:=table1.fields[j].asstring;
table1.next;
end;
end;
procedure TForm1.WriteToExcel(adataset: TDataSet; selrows: TBookList);
var oexcel: OleVariant;
i,j: integer;
begin
try
oexcel:=GetActiveOleObject('Excel.Application');
except
try
oexcel:=CreateOleObject('Excel.Application');
except
MessageDlg('无法启动EXCEL程序。'+#13+'请确定该程序已正确安装!',mtInformation,[mbOK],0);
exit;
end;
end;
oexcel.WorkBooks.Add;
with adataset do begin
for i:=1 to FieldCount do
oexcel.WorkSheets['Sheet1'].Cells[1,i].Value:=Fields[i-1].FieldName;
if selrows<>nil then begin
for j:=2 to selrows.Count+1 do begin
GotoBook(pointer(selrows.Items[j-2]));
for i:=1 to FieldCount do begin
Application.ProcessMessages;
oexcel.WorkSheets['Sheet1'].Cells[j,i].Value:=Fields[i-1].AsString;
end;
end;
end else begin
j:=2;
First;
while not eof do begin
for i:=1 to FieldCount do begin
Application.ProcessMessages;
oexcel.WorkSheets['Sheet1'].Cells[j,i].Value:=Fields[i-1].AsString;
end;
j:=j+1;
Next;
end;
end;
end;
oexcel.Visible:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
WriteToExcel(table1,dbgrid1.SelectedRows);
end;procedure TForm1.Button3Click(Sender: TObject);
var c,r,i,j : integer ;
app : Olevariant ;
TempFileName,ResultFileName : String ;
begin
try
app := CreateOLEObject('Excel.application') ;
except
Messagedlg('Excel没有正确安装!',mterror,[mbok],0);
exit ;
end ;
TempFileName := 'test' ;
app.Workbooks.add ;
app.Visible := false ; dbgrid1.DataSource.DataSet.First;
// DBGResult.DataSource.DataSet.First ;
c:=dbgrid1.DataSource.DataSet.FieldCount ;
r:=dbgrid1.DataSource.DataSet.RecordCount ; for i:=0 to c-1 do
app.cells(1,1+i):= dbgrid1.DataSource.DataSet.Fields[i].DisplayLabel ;
for j:=1 to r do
begin
for i:=0 to c-1 do
app.cells(j+1,1+i):= dbgrid1.DataSource.DataSet.Fields[i].AsString ; dbgrid1.DataSource.DataSet.Next ;
end ; ResultFileName := TempFileName ;
if ResultFileName='' then ResultFileName:='自动报表' ;
if FileExists(ExtractFilePath(Application.EXEName)+ResultFileName+'.xls') then
DeleteFile(ExtractFilePath(Application.EXEName)+ResultFileName+'.xls') ; app.Activeworkbook.saveas(ExtractFilePath(Application.EXEName)+ResultFileName+'.xls') ;
app.Activeworkbook.close(false) ;
app.quit ;
app:=unassigned ;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
table1.Filtered:=false;
table1.Filter:='空调名称='+QuotedStr('海尔空调');
table1.Filtered:=true;
end;end.