我想通过这个程序,可以选择性的将excel工作薄中的多张工作表导入到access数据库中,不知道下面的代码的思路是否正确,调试的时候没有成功,我是个新手,大家不要骂我啊,谢谢大家!
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls,
ExtCtrls, ComCtrls;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
Label2: TLabel;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
ADODataSet2: TADODataSet;
DataSource1: TDataSource;
ADOConnection2: TADOConnection;
OpenDialog2: TOpenDialog;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
const
Constring='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Mode=Read;Extended Properties=Excel 8.0;Persist Security Info=False'; //连接excel
var
i:integer;
TableName:string;
begin
if Opendialog2.Execute then
begin
adodataset2.Close;
adoconnection2.Close;
adoconnection2.ConnectionString:=Format(constring,[Opendialog2.FileName]);//是动态获得excel的地址吧?
adoconnection2.Open;
adoconnection2.GetTableNames(Form2.lst1.Items,false);//此语句是将excel工作薄中的表传到form2中的listbox中吧
if Form2.ShowModal=mrok then
begin
for i:=0 to Form2.lst1.Count-1 do
begin
if Form2.lst1.Selected[i] then
TableName:=Form2.lst1.Items.Strings[i]; //相当于在form2的listbox中,选择工作薄中的表,
end;
if TableName<>'' then
begin
adodataset2.Close;
adodataset2.CommandText:='select * from ['+TableName+']';
adodataset2.Open;
label2.caption:='共'+inttostr(adodataset2.RecordCount)+'条记录!';
end;
end;
end;ADOquery1.Close; //query1直接连access数据库
ADOquery1.Sql.Text := 'SELECT top 1 * FROM sp where 1=0'; //sp为数据表名,此语句相当于打开一张空表, ADOquery1.Open;
if ADOquery2.RecordCount > 0 then
begin ADOquery2.First;
for i := 1 to ADOquery2.RecordCount do //将excel表中的数据传到数据表中.
begin
ADOquery1.Append;
ADOquery1.FieldValues['编号']:=ADOquery2.FieldValues['编号'];
ADOquery1.FieldValues['名称']:=ADOquery2.FieldValues['名称'];
ADOquery1.FieldValues['价格']:=ADOquery2.FieldValues['价格'];
ADOquery1.Post; ADOquery2.Next;
end; end; MessageBox(Handle,'导入完成!','完成!',MB_OK+MB_ICONINFORMATION);//这儿应该没错啊,怎么报错呢?
except
MessageBox(Handle,'失败!','信息!',MB_OK+MB_ICONINFORMATION);
end;
end.这是报的错:
[Error] Unit1.pas(96): 'END' expected but 'EXCEPT' found
[Fatal Error] Project2.dpr(6): Could not compile used unit 'Unit1.pas'
我试过多次了,才请教大家.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls,
ExtCtrls, ComCtrls;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
Label2: TLabel;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
ADODataSet2: TADODataSet;
DataSource1: TDataSource;
ADOConnection2: TADOConnection;
OpenDialog2: TOpenDialog;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
const
Constring='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Mode=Read;Extended Properties=Excel 8.0;Persist Security Info=False'; //连接excel
var
i:integer;
TableName:string;
begin
if Opendialog2.Execute then
begin
adodataset2.Close;
adoconnection2.Close;
adoconnection2.ConnectionString:=Format(constring,[Opendialog2.FileName]);//是动态获得excel的地址吧?
adoconnection2.Open;
adoconnection2.GetTableNames(Form2.lst1.Items,false);//此语句是将excel工作薄中的表传到form2中的listbox中吧
if Form2.ShowModal=mrok then
begin
for i:=0 to Form2.lst1.Count-1 do
begin
if Form2.lst1.Selected[i] then
TableName:=Form2.lst1.Items.Strings[i]; //相当于在form2的listbox中,选择工作薄中的表,
end;
if TableName<>'' then
begin
adodataset2.Close;
adodataset2.CommandText:='select * from ['+TableName+']';
adodataset2.Open;
label2.caption:='共'+inttostr(adodataset2.RecordCount)+'条记录!';
end;
end;
end;ADOquery1.Close; //query1直接连access数据库
ADOquery1.Sql.Text := 'SELECT top 1 * FROM sp where 1=0'; //sp为数据表名,此语句相当于打开一张空表, ADOquery1.Open;
if ADOquery2.RecordCount > 0 then
begin ADOquery2.First;
for i := 1 to ADOquery2.RecordCount do //将excel表中的数据传到数据表中.
begin
ADOquery1.Append;
ADOquery1.FieldValues['编号']:=ADOquery2.FieldValues['编号'];
ADOquery1.FieldValues['名称']:=ADOquery2.FieldValues['名称'];
ADOquery1.FieldValues['价格']:=ADOquery2.FieldValues['价格'];
ADOquery1.Post; ADOquery2.Next;
end; end; MessageBox(Handle,'导入完成!','完成!',MB_OK+MB_ICONINFORMATION);//这儿应该没错啊,怎么报错呢?
except
MessageBox(Handle,'失败!','信息!',MB_OK+MB_ICONINFORMATION);
end;
end.这是报的错:
[Error] Unit1.pas(96): 'END' expected but 'EXCEPT' found
[Fatal Error] Project2.dpr(6): Could not compile used unit 'Unit1.pas'
我试过多次了,才请教大家.
解决方案 »
- 请教如何把1,2,3..换成一,二,三.....
- 类的问题,在线等
- 如何把doc或者rtf文档保存到SQL SERVER数据库的varbinary字段?
- 通过modem发送数据的问题。搞不定了,高手们来帮帮我啊!呜呜呜...
- delphi 中如何制作word 报表
- 哪位有《杰克韦尔奇自传》,提供下载连接也可!100分
- 为什么我定义的Class的Private变量也可以被外部修改,在.后面也会列出来?
- :) 送分了 在用opendialog打开一个*.db文件,要对query进行些什么设置.
- 求delphi使用正则提取指定table的表达式?
- 请问NMSMTP控件发邮件时,怎么添加附件?
- windows路由NAT功能中 远程访问客户端的获取办法
- 在delphi中如何写sql语句统计表中出现一样的数字!
try
except
end;
try
finally
end;
异常要这样用
控制一下代码缩进吧,这样看了实在累
好像漏了try!
Unit1.pas 单元内期望出现End,却出现了Except,也就是说你的Except之前的Begin End没有成对出现,少了一个End。
[Fatal Error] Project2.dpr(6): Could not compile used unit 'Unit1.pas'
这个错误是因为前面出错,导致工程不能被编译成功。
procedure TForm1.Button2Click(Sender: TObject);
const
Constring='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Mode=Read;Extended Properties=Excel 8.0;Persist Security Info=False'; //连接excel
var
i:integer;
TableName:string;
begin
if Opendialog2.Execute then
begin
adodataset2.Close;
adoconnection2.Close;
adoconnection2.ConnectionString:=Format(constring,[Opendialog2.FileName]);//是动态获得excel的地址吧?
adoconnection2.Open;
adoconnection2.GetTableNames(Form2.lst1.Items,false);//此语句是将excel工作薄中的表传到form2中的listbox中吧
if Form2.ShowModal=mrok then
begin
for i:=0 to Form2.lst1.Count-1 do
begin
if Form2.lst1.Selected[i] then
TableName:=Form2.lst1.Items.Strings[i]; //相当于在form2的listbox中,选择工作薄中的表,
end;
if TableName <>'' then
begin
adodataset2.Close;
adodataset2.CommandText:='select * from ['+TableName+']';
adodataset2.Open;
label2.caption:='共'+inttostr(adodataset2.RecordCount)+'条记录!';
end;
end;
end; ADOquery1.Close; //query1直接连access数据库
ADOquery1.Sql.Text := 'SELECT top 1 * FROM sp where 1=0'; //sp为数据表名,此语句相当于打开一张空表, ADOquery1.Open;
if ADOquery2.RecordCount > 0 then
begin
ADOquery2.First;
for i := 1 to ADOquery2.RecordCount do //将excel表中的数据传到数据表中.
begin
ADOquery1.Append;
ADOquery1.FieldValues['编号']:=ADOquery2.FieldValues['编号'];
ADOquery1.FieldValues['名称']:=ADOquery2.FieldValues['名称'];
ADOquery1.FieldValues['价格']:=ADOquery2.FieldValues['价格'];
ADOquery1.Post; ADOquery2.Next;
end;
end;
MessageBox(Handle,'导入完成!','完成!',MB_OK+MB_ICONINFORMATION);//这儿应该没错啊,怎么报错呢?
except //打这句去掉,你前面没有Try语句,哪里来的Except?
MessageBox(Handle,'失败!','信息!',MB_OK+MB_ICONINFORMATION);
end;