请高手指点:如何搭建相应的软件环境,不浪费时间就说说大概要做些什么,然后如何用ADO将数据导入,说说主要做些什么。多谢了。!~~
13 个解决方案
#1
读取Excel可以通过SQL、OLE接口等多种方法,具体请搜索以前帖子
#2
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,ComOBJ;
type
PPerson = ^TPerson;
TPerson = record
FCode: String;
FName: string;
end;
type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
btn1:TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//procedure Btn1Click(Sender: TObject);
//procedure Button2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
FPerson: PPerson;
FPersonList: TList;
function LoadExcel: Boolean;
function SaveToDB: Boolean;
//function DBToExcel: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.LoadExcel: Boolean;
var
RangeMatrix: Variant;
OpenDialog1: TOpenDialog;
iRE: integer;
IN_TYPE,vStockName: String;
sStockName,sCompany: String;
i: Integer;
MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
Col1,Col2: String;
begin
OpenDialog1:=TOpenDialog.Create(nil);
try
//OpenDialog1.FileName:='*.csv;*.xls';
OpenDialog1.FileName:='*.xls';
if not OpenDialog1.Execute then
begin
Result := false;
exit;
end;
MsExcel := CreateOleObject('Excel.Application');
MsExcelWorkBook := msExcel.Workbooks.Open(OpenDialog1.FileName);
MsExcelWorkSheet := msExcel.Worksheets.Item[1];
except
Result := false;
Exit;
end;
//开始从EXCEL文件读取相关的信息
try
try
Application.ProcessMessages;
for i := 1 to MsExcelWorkSheet.Rows.Count do
begin
if trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value)='' then break;
New(FPerson);
FPerson.FCode := trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value);
FPerson.FName := trim(MsExcelWorkSheet.Range['B' + IntToStr(i)].Value);
FPersonList.Add(FPerson);
end;
except
Result := false;
Exit;
end;
finally
MsExcel.WorkBooks.Close;
end;
Result := true;
MsExcel.Quit;
OpenDialog1.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//创建一个list列表,以用来存取表数据
FPersonList := TList.create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FPersonList.Free;//释放资源
end;
function TForm1.SaveToDB: Boolean;
var
i: integer;
begin
con1.BeginTrans;
try
for i:= 0 to FPersonList.Count-1 do
begin
New(FPerson);
FPerson := FPersonList[i];
with qry1 do
begin
close;
sql.Text := ' insert into ttable(name,ye,ywy,bm,bz) values(:name,:ye,:ywy,:bm,:bz)';
Parameters.ParamByName('name').Value := FPerson.FName;
parameters.ParamByName('ye').Value := FPerson.FCode;
parameters.ParamByName('ywy').Value := FPerson.FCode;
parameters.ParamByName('bm').Value := FPerson.FCode;
parameters.ParamByName('bz').Value := FPerson.FCode;
ExecSql;
end;
end;
con1.CommitTrans;
Result := true;
except
con1.RollbackTrans;
Result := false;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
LoadExcel;
SaveToDB;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
//DBToExcel;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,ComOBJ;
type
PPerson = ^TPerson;
TPerson = record
FCode: String;
FName: string;
end;
type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
btn1:TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//procedure Btn1Click(Sender: TObject);
//procedure Button2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
FPerson: PPerson;
FPersonList: TList;
function LoadExcel: Boolean;
function SaveToDB: Boolean;
//function DBToExcel: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.LoadExcel: Boolean;
var
RangeMatrix: Variant;
OpenDialog1: TOpenDialog;
iRE: integer;
IN_TYPE,vStockName: String;
sStockName,sCompany: String;
i: Integer;
MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
Col1,Col2: String;
begin
OpenDialog1:=TOpenDialog.Create(nil);
try
//OpenDialog1.FileName:='*.csv;*.xls';
OpenDialog1.FileName:='*.xls';
if not OpenDialog1.Execute then
begin
Result := false;
exit;
end;
MsExcel := CreateOleObject('Excel.Application');
MsExcelWorkBook := msExcel.Workbooks.Open(OpenDialog1.FileName);
MsExcelWorkSheet := msExcel.Worksheets.Item[1];
except
Result := false;
Exit;
end;
//开始从EXCEL文件读取相关的信息
try
try
Application.ProcessMessages;
for i := 1 to MsExcelWorkSheet.Rows.Count do
begin
if trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value)='' then break;
New(FPerson);
FPerson.FCode := trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value);
FPerson.FName := trim(MsExcelWorkSheet.Range['B' + IntToStr(i)].Value);
FPersonList.Add(FPerson);
end;
except
Result := false;
Exit;
end;
finally
MsExcel.WorkBooks.Close;
end;
Result := true;
MsExcel.Quit;
OpenDialog1.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//创建一个list列表,以用来存取表数据
FPersonList := TList.create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FPersonList.Free;//释放资源
end;
function TForm1.SaveToDB: Boolean;
var
i: integer;
begin
con1.BeginTrans;
try
for i:= 0 to FPersonList.Count-1 do
begin
New(FPerson);
FPerson := FPersonList[i];
with qry1 do
begin
close;
sql.Text := ' insert into ttable(name,ye,ywy,bm,bz) values(:name,:ye,:ywy,:bm,:bz)';
Parameters.ParamByName('name').Value := FPerson.FName;
parameters.ParamByName('ye').Value := FPerson.FCode;
parameters.ParamByName('ywy').Value := FPerson.FCode;
parameters.ParamByName('bm').Value := FPerson.FCode;
parameters.ParamByName('bz').Value := FPerson.FCode;
ExecSql;
end;
end;
con1.CommitTrans;
Result := true;
except
con1.RollbackTrans;
Result := false;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
LoadExcel;
SaveToDB;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
//DBToExcel;
end;
end.
#3
--启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
SELECT * INTO tablename FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Excel 8.0;DATABASE=E:\aa.xls',Sheet1$)
--关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
SELECT * INTO tablename FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Excel 8.0;DATABASE=E:\aa.xls',Sheet1$)
--关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
#4
我上面讲的是在查询分析器里执行的
#5
我晕了,LS的你弄的这么长的我刚才也搜索到过,但是我现在很想知道我现在需要做的就是搭建基本的软件环境方面,就比如你要编写delphi程序就得先下载delphi编译软件,然后安装,并且安装好,然后新建一个应用程序之类的,然后才可以编写delphi程序。我现在就是对这个流程很不懂,请高手给告诉下基本的流程。
#6
我说的是3楼
#7
都没有人帮忙指点一下。悲伤ing
#8
你先下个delphi,安装好,然后找本书,先看一下。
因为搭建delphi基本的软件环境不是在这里一两句就讲得清的。
你们老师也够水平,将EXCEL中的数据导入到SQL SERVER数据库中,要从没用过DELPHI的人来做。
从学习角度讲,他应该教你们先怎样用DELPHI
从应用角度讲,3楼的方法就可以,除了MS OFFICE和SQLSERVER,什么都不要装了。
因为搭建delphi基本的软件环境不是在这里一两句就讲得清的。
你们老师也够水平,将EXCEL中的数据导入到SQL SERVER数据库中,要从没用过DELPHI的人来做。
从学习角度讲,他应该教你们先怎样用DELPHI
从应用角度讲,3楼的方法就可以,除了MS OFFICE和SQLSERVER,什么都不要装了。
#9
在转换的时候,Excel中的字段在ADO读取出来的时候都是字符串,你可以根据SQL Server中表字段的数据类型进行相应的转换就可以了。
#10
OPENROWSET
#11
我觉得是最好,先把数据导入到一个网格控件,再保存到数据库中
#12
if UpperCase(ExtractFileExt(Trim(NewAccountMonthWizard.EdtXlsFile.Text)))='XLSX' then
begin
//为excel 2007 文件
dm.ADOConnUser.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+Trim(NewAccountMonthWizard.EdtXlsFile.Text) +';Extended Properties=Excel 8.0;Persist Security Info=False';
end
else
begin
dm.ADOConnUser.ConnectionString:='Provider=Microsoft.ACE.OLEDB.12.0;Password='''';Data Source='+Trim(NewAccountMonthWizard.EdtXlsFile.Text) +';Extended Properties=''Excel 12.0;IMEX=1;HDR=YES'';Persist Security Info=True;';
end;
直接用ADO连到EXCEL 后,再读取
#13
然后 select * from [sheel1$],[shees2#] where 直接当数据库表来查询就好了.............................
这两天我也做导EXCEL到SQL SERVER 的,QQ:121810267 欢迎交流
这两天我也做导EXCEL到SQL SERVER 的,QQ:121810267 欢迎交流
#1
读取Excel可以通过SQL、OLE接口等多种方法,具体请搜索以前帖子
#2
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,ComOBJ;
type
PPerson = ^TPerson;
TPerson = record
FCode: String;
FName: string;
end;
type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
btn1:TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//procedure Btn1Click(Sender: TObject);
//procedure Button2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
FPerson: PPerson;
FPersonList: TList;
function LoadExcel: Boolean;
function SaveToDB: Boolean;
//function DBToExcel: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.LoadExcel: Boolean;
var
RangeMatrix: Variant;
OpenDialog1: TOpenDialog;
iRE: integer;
IN_TYPE,vStockName: String;
sStockName,sCompany: String;
i: Integer;
MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
Col1,Col2: String;
begin
OpenDialog1:=TOpenDialog.Create(nil);
try
//OpenDialog1.FileName:='*.csv;*.xls';
OpenDialog1.FileName:='*.xls';
if not OpenDialog1.Execute then
begin
Result := false;
exit;
end;
MsExcel := CreateOleObject('Excel.Application');
MsExcelWorkBook := msExcel.Workbooks.Open(OpenDialog1.FileName);
MsExcelWorkSheet := msExcel.Worksheets.Item[1];
except
Result := false;
Exit;
end;
//开始从EXCEL文件读取相关的信息
try
try
Application.ProcessMessages;
for i := 1 to MsExcelWorkSheet.Rows.Count do
begin
if trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value)='' then break;
New(FPerson);
FPerson.FCode := trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value);
FPerson.FName := trim(MsExcelWorkSheet.Range['B' + IntToStr(i)].Value);
FPersonList.Add(FPerson);
end;
except
Result := false;
Exit;
end;
finally
MsExcel.WorkBooks.Close;
end;
Result := true;
MsExcel.Quit;
OpenDialog1.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//创建一个list列表,以用来存取表数据
FPersonList := TList.create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FPersonList.Free;//释放资源
end;
function TForm1.SaveToDB: Boolean;
var
i: integer;
begin
con1.BeginTrans;
try
for i:= 0 to FPersonList.Count-1 do
begin
New(FPerson);
FPerson := FPersonList[i];
with qry1 do
begin
close;
sql.Text := ' insert into ttable(name,ye,ywy,bm,bz) values(:name,:ye,:ywy,:bm,:bz)';
Parameters.ParamByName('name').Value := FPerson.FName;
parameters.ParamByName('ye').Value := FPerson.FCode;
parameters.ParamByName('ywy').Value := FPerson.FCode;
parameters.ParamByName('bm').Value := FPerson.FCode;
parameters.ParamByName('bz').Value := FPerson.FCode;
ExecSql;
end;
end;
con1.CommitTrans;
Result := true;
except
con1.RollbackTrans;
Result := false;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
LoadExcel;
SaveToDB;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
//DBToExcel;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,ComOBJ;
type
PPerson = ^TPerson;
TPerson = record
FCode: String;
FName: string;
end;
type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
btn1:TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//procedure Btn1Click(Sender: TObject);
//procedure Button2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
FPerson: PPerson;
FPersonList: TList;
function LoadExcel: Boolean;
function SaveToDB: Boolean;
//function DBToExcel: Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.LoadExcel: Boolean;
var
RangeMatrix: Variant;
OpenDialog1: TOpenDialog;
iRE: integer;
IN_TYPE,vStockName: String;
sStockName,sCompany: String;
i: Integer;
MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
Col1,Col2: String;
begin
OpenDialog1:=TOpenDialog.Create(nil);
try
//OpenDialog1.FileName:='*.csv;*.xls';
OpenDialog1.FileName:='*.xls';
if not OpenDialog1.Execute then
begin
Result := false;
exit;
end;
MsExcel := CreateOleObject('Excel.Application');
MsExcelWorkBook := msExcel.Workbooks.Open(OpenDialog1.FileName);
MsExcelWorkSheet := msExcel.Worksheets.Item[1];
except
Result := false;
Exit;
end;
//开始从EXCEL文件读取相关的信息
try
try
Application.ProcessMessages;
for i := 1 to MsExcelWorkSheet.Rows.Count do
begin
if trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value)='' then break;
New(FPerson);
FPerson.FCode := trim(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value);
FPerson.FName := trim(MsExcelWorkSheet.Range['B' + IntToStr(i)].Value);
FPersonList.Add(FPerson);
end;
except
Result := false;
Exit;
end;
finally
MsExcel.WorkBooks.Close;
end;
Result := true;
MsExcel.Quit;
OpenDialog1.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//创建一个list列表,以用来存取表数据
FPersonList := TList.create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FPersonList.Free;//释放资源
end;
function TForm1.SaveToDB: Boolean;
var
i: integer;
begin
con1.BeginTrans;
try
for i:= 0 to FPersonList.Count-1 do
begin
New(FPerson);
FPerson := FPersonList[i];
with qry1 do
begin
close;
sql.Text := ' insert into ttable(name,ye,ywy,bm,bz) values(:name,:ye,:ywy,:bm,:bz)';
Parameters.ParamByName('name').Value := FPerson.FName;
parameters.ParamByName('ye').Value := FPerson.FCode;
parameters.ParamByName('ywy').Value := FPerson.FCode;
parameters.ParamByName('bm').Value := FPerson.FCode;
parameters.ParamByName('bz').Value := FPerson.FCode;
ExecSql;
end;
end;
con1.CommitTrans;
Result := true;
except
con1.RollbackTrans;
Result := false;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
LoadExcel;
SaveToDB;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
//DBToExcel;
end;
end.
#3
--启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
SELECT * INTO tablename FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Excel 8.0;DATABASE=E:\aa.xls',Sheet1$)
--关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
SELECT * INTO tablename FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Excel 8.0;DATABASE=E:\aa.xls',Sheet1$)
--关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
#4
我上面讲的是在查询分析器里执行的
#5
我晕了,LS的你弄的这么长的我刚才也搜索到过,但是我现在很想知道我现在需要做的就是搭建基本的软件环境方面,就比如你要编写delphi程序就得先下载delphi编译软件,然后安装,并且安装好,然后新建一个应用程序之类的,然后才可以编写delphi程序。我现在就是对这个流程很不懂,请高手给告诉下基本的流程。
#6
我说的是3楼
#7
都没有人帮忙指点一下。悲伤ing
#8
你先下个delphi,安装好,然后找本书,先看一下。
因为搭建delphi基本的软件环境不是在这里一两句就讲得清的。
你们老师也够水平,将EXCEL中的数据导入到SQL SERVER数据库中,要从没用过DELPHI的人来做。
从学习角度讲,他应该教你们先怎样用DELPHI
从应用角度讲,3楼的方法就可以,除了MS OFFICE和SQLSERVER,什么都不要装了。
因为搭建delphi基本的软件环境不是在这里一两句就讲得清的。
你们老师也够水平,将EXCEL中的数据导入到SQL SERVER数据库中,要从没用过DELPHI的人来做。
从学习角度讲,他应该教你们先怎样用DELPHI
从应用角度讲,3楼的方法就可以,除了MS OFFICE和SQLSERVER,什么都不要装了。
#9
在转换的时候,Excel中的字段在ADO读取出来的时候都是字符串,你可以根据SQL Server中表字段的数据类型进行相应的转换就可以了。
#10
OPENROWSET
#11
我觉得是最好,先把数据导入到一个网格控件,再保存到数据库中
#12
if UpperCase(ExtractFileExt(Trim(NewAccountMonthWizard.EdtXlsFile.Text)))='XLSX' then
begin
//为excel 2007 文件
dm.ADOConnUser.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+Trim(NewAccountMonthWizard.EdtXlsFile.Text) +';Extended Properties=Excel 8.0;Persist Security Info=False';
end
else
begin
dm.ADOConnUser.ConnectionString:='Provider=Microsoft.ACE.OLEDB.12.0;Password='''';Data Source='+Trim(NewAccountMonthWizard.EdtXlsFile.Text) +';Extended Properties=''Excel 12.0;IMEX=1;HDR=YES'';Persist Security Info=True;';
end;
直接用ADO连到EXCEL 后,再读取
#13
然后 select * from [sheel1$],[shees2#] where 直接当数据库表来查询就好了.............................
这两天我也做导EXCEL到SQL SERVER 的,QQ:121810267 欢迎交流
这两天我也做导EXCEL到SQL SERVER 的,QQ:121810267 欢迎交流