就是想用Delphi 的 AdoQuery连接 Sql Server 2000 ,写程序把用Excel做好的
表导入到Sql中请你解释一下这个代码可以吗 ?????
//将Excel中的数据导入StringGrid中
Function ExcelToStringGrid(var StrGrid:TStringGrid;ExcelFileName:string):String;
Function IsInIt(EmployeeNum:string):boolean;
var
i:integer;
ret:boolean;
begin
ret:=False;
for i:=0 to StrGrid.RowCount-1 do
begin
if EmployeeNum=trim(StrGrid.Cells[0,i]) then
begin
ret:=True;
break;
end;
end;
result:=ret;
end;
var
ExcelApplication: TExcelApplication;
ExcelWorkbook: TExcelWorkbook;
ExcelWorksheet: TExcelWorksheet;
count,NowRow,i:integer;
ret:String;
begin
ret:='导入成功';
try
screen.Cursor :=crSQLWait;
if Not FileExists(ExcelFileName) then
begin
//文件不存在
ret:='文件不存在';
exit;
end;
//测试EXCEL是否安装
Try
ExcelApplication:=nil;
ExcelWorkbook:=nil;
ExcelWorksheet:=nil;
ExcelApplication:=TExcelApplication.Create(nil);
ExcelWorkbook :=TExcelWorkbook.Create(nil);
ExcelWorksheet :=TExcelWorksheet.Create(nil);
ExcelApplication.Connect;//EXCEL应用程序
Except
//连接Excel失败
ret:='连接Excel失败';
exit;
End;
//打开Excel时是否显示
ExcelApplication.Visible[0]:=False;
ExcelApplication.DisplayAlerts[0] :=False;
//打开指定表格
try
ExcelApplication.Workbooks.Open(ExcelFileName,
null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件
except
//打开报表失败
ret:='打开报表失败';
exit;
end;
try
//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorkbook.ConnectTo(ExcelApplication.Workbooks[1]);
//Excelworksheet1与Excelworkbook1建立连接
ExcelWorksheet.ConnectTo(ExcelWorkbook.Worksheets[1] as _Worksheet);
except
ret:='连接报表失败';
exit;
end; {//判断文件是否已导入
if Trim(ExcelWorksheet.Cells.Item[2,1])='导入完毕' then
begin
//该报表已导入
ret:='该报表已导入';
exit;
end;} try
//处理报表
Count:=3;
StrGrid.RowCount :=2;
StrGrid.FixedRows :=1;
StrGrid.ColCount :=3;
StrGrid.FixedCols :=0;
for i:=0 to StrGrid.ColCount-1 do
StrGrid.Cells[i,1]:=''; While Trim(ExcelWorksheet.Cells.Item[Count,1])<>'*****' do
begin
Application.ProcessMessages;
//如果超过一千则退出
if Count>=1003 then
begin
Break;
end;
//导入数据
if (Trim(ExcelWorksheet.Cells.Item[Count,1])<>'') and
(Trim(ExcelWorksheet.Cells.Item[Count,2])<>'') and
(Not IsInIt(Trim(ExcelWorksheet.Cells.Item[Count,1]))) then
begin
if (StrGrid.RowCount =2) and (StrGrid.Cells[0,1]='') then
begin
NowRow:=1;
end
else
begin
StrGrid.RowCount :=StrGrid.RowCount +1;
NowRow:=StrGrid.RowCount-1;
end;
//导入编号
if length(Trim(ExcelWorksheet.Cells.Item[Count,1],True))>12 then
StrGrid.Cells[0,NowRow]:=leftstr(Trim(ExcelWorksheet.Cells.Item[Count,1],True),12)
else
StrGrid.Cells[0,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,1],True);
//导入姓名
if length(Trim(ExcelWorksheet.Cells.Item[Count,2],True))>10 then
StrGrid.Cells[1,NowRow]:=Leftstr(Trim(ExcelWorksheet.Cells.Item[Count,2],True),10)
else
StrGrid.Cells[1,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,2],True);
//导入性别
if Trim(ExcelWorksheet.Cells.Item[Count,3])<>'男' then
StrGrid.Cells[2,NowRow]:='女'
else
StrGrid.Cells[2,NowRow]:='男';
end;
Inc(Count);
end; //给已导入的文件进行标识
//ExcelWorksheet.Cells.Item[2,1]:='导入完毕';
//ExcelWorkbook.Save;
//导入成功
ret:='导入成功';
except
ret:='导入出现异常错误';
end;
finally
ExcelApplication.Disconnect;
ExcelApplication.Quit;
if ExcelApplication<>nil then
begin
ExcelApplication.Free;
ExcelApplication:=nil;
end;
if ExcelWorkbook<>nil then
begin
ExcelWorkbook.Free;
ExcelWorkbook:=nil;
end;
if ExcelWorksheet<>nil then
begin
ExcelWorksheet.Free;
ExcelWorksheet:=nil;
end;
result:=ret;
screen.Cursor :=crDefault;
end;
end;
表导入到Sql中请你解释一下这个代码可以吗 ?????
//将Excel中的数据导入StringGrid中
Function ExcelToStringGrid(var StrGrid:TStringGrid;ExcelFileName:string):String;
Function IsInIt(EmployeeNum:string):boolean;
var
i:integer;
ret:boolean;
begin
ret:=False;
for i:=0 to StrGrid.RowCount-1 do
begin
if EmployeeNum=trim(StrGrid.Cells[0,i]) then
begin
ret:=True;
break;
end;
end;
result:=ret;
end;
var
ExcelApplication: TExcelApplication;
ExcelWorkbook: TExcelWorkbook;
ExcelWorksheet: TExcelWorksheet;
count,NowRow,i:integer;
ret:String;
begin
ret:='导入成功';
try
screen.Cursor :=crSQLWait;
if Not FileExists(ExcelFileName) then
begin
//文件不存在
ret:='文件不存在';
exit;
end;
//测试EXCEL是否安装
Try
ExcelApplication:=nil;
ExcelWorkbook:=nil;
ExcelWorksheet:=nil;
ExcelApplication:=TExcelApplication.Create(nil);
ExcelWorkbook :=TExcelWorkbook.Create(nil);
ExcelWorksheet :=TExcelWorksheet.Create(nil);
ExcelApplication.Connect;//EXCEL应用程序
Except
//连接Excel失败
ret:='连接Excel失败';
exit;
End;
//打开Excel时是否显示
ExcelApplication.Visible[0]:=False;
ExcelApplication.DisplayAlerts[0] :=False;
//打开指定表格
try
ExcelApplication.Workbooks.Open(ExcelFileName,
null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件
except
//打开报表失败
ret:='打开报表失败';
exit;
end;
try
//ExcelWorkbook1与Eexcelapplication1建立连接
ExcelWorkbook.ConnectTo(ExcelApplication.Workbooks[1]);
//Excelworksheet1与Excelworkbook1建立连接
ExcelWorksheet.ConnectTo(ExcelWorkbook.Worksheets[1] as _Worksheet);
except
ret:='连接报表失败';
exit;
end; {//判断文件是否已导入
if Trim(ExcelWorksheet.Cells.Item[2,1])='导入完毕' then
begin
//该报表已导入
ret:='该报表已导入';
exit;
end;} try
//处理报表
Count:=3;
StrGrid.RowCount :=2;
StrGrid.FixedRows :=1;
StrGrid.ColCount :=3;
StrGrid.FixedCols :=0;
for i:=0 to StrGrid.ColCount-1 do
StrGrid.Cells[i,1]:=''; While Trim(ExcelWorksheet.Cells.Item[Count,1])<>'*****' do
begin
Application.ProcessMessages;
//如果超过一千则退出
if Count>=1003 then
begin
Break;
end;
//导入数据
if (Trim(ExcelWorksheet.Cells.Item[Count,1])<>'') and
(Trim(ExcelWorksheet.Cells.Item[Count,2])<>'') and
(Not IsInIt(Trim(ExcelWorksheet.Cells.Item[Count,1]))) then
begin
if (StrGrid.RowCount =2) and (StrGrid.Cells[0,1]='') then
begin
NowRow:=1;
end
else
begin
StrGrid.RowCount :=StrGrid.RowCount +1;
NowRow:=StrGrid.RowCount-1;
end;
//导入编号
if length(Trim(ExcelWorksheet.Cells.Item[Count,1],True))>12 then
StrGrid.Cells[0,NowRow]:=leftstr(Trim(ExcelWorksheet.Cells.Item[Count,1],True),12)
else
StrGrid.Cells[0,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,1],True);
//导入姓名
if length(Trim(ExcelWorksheet.Cells.Item[Count,2],True))>10 then
StrGrid.Cells[1,NowRow]:=Leftstr(Trim(ExcelWorksheet.Cells.Item[Count,2],True),10)
else
StrGrid.Cells[1,NowRow]:=Trim(ExcelWorksheet.Cells.Item[Count,2],True);
//导入性别
if Trim(ExcelWorksheet.Cells.Item[Count,3])<>'男' then
StrGrid.Cells[2,NowRow]:='女'
else
StrGrid.Cells[2,NowRow]:='男';
end;
Inc(Count);
end; //给已导入的文件进行标识
//ExcelWorksheet.Cells.Item[2,1]:='导入完毕';
//ExcelWorkbook.Save;
//导入成功
ret:='导入成功';
except
ret:='导入出现异常错误';
end;
finally
ExcelApplication.Disconnect;
ExcelApplication.Quit;
if ExcelApplication<>nil then
begin
ExcelApplication.Free;
ExcelApplication:=nil;
end;
if ExcelWorkbook<>nil then
begin
ExcelWorkbook.Free;
ExcelWorkbook:=nil;
end;
if ExcelWorksheet<>nil then
begin
ExcelWorksheet.Free;
ExcelWorksheet:=nil;
end;
result:=ret;
screen.Cursor :=crDefault;
end;
end;
解决方案 »
- 简单问题
- 专门讲解用c或c++写的开源软件的书籍
- 还是时间控件问题
- 如何在DBGrid中显示百分数,数据库为SQL sever
- 关于sendmessage的问题(我这个问题好像还没人提过)
- 向Raptor(猛禽)/pazee(耙子)/太可怕 等各位老大求助 Help me~
- 为何在DELPHI6下无法使用TDBLookUpCombobox组件?竟然连text属性都没有,郁闷。
- 内存数据段搜索怎样做,我做了好久都没有做出来,就像游戏修改内存数据的那种
- 请问怎样在delphi中获取一个文件的路径,
- 我的表的字段没法替换????
- 本窗口Hide后,如何控制刷新背景上的此部分?
- 大家帮忙看看这段代码,为什么执行不过去……
fwjindream(梦中飞) 来了就帮忙解释一下哈,我看不懂哈 谢谢
就是想用Delphi 的 AdoQuery连接 Sql Server 2000 ,写程序把用Excel做好的
表导入到Sql中 你可不可以帮我指点啊,给点提示或者程序嘛
谢谢