5,388
社区成员
发帖
与我相关
我的任务
分享
try
i:=2;
//j:=excelApplication1.Rows.Count;
j:=excelWorksheet1.UsedRange[65536].Rows.Count+1;
if j<2 then
begin
ShowMessage('表格数据为空!');
Exit;
end;
//try将这个try移到上面试下。。。。。。。。。。。。。。。。。。。。。。。
// try
while i<=j do
begin
if Trim(excelWorksheet1.Cells.Item[i,2])<>'' then
begin
if not AddtoSQL(excelWorksheet1.Cells.Item[i,1], //<span style="color: #FF0000;">这个地方会报错,因为数据格式不下确.</span>
Trim(excelWorksheet1.Cells.Item[i,2]),
trim(excelWorksheet1.Cells.Item[i,3]),
Trim(excelWorksheet1.Cells.Item[i,4]),
StrToFloat(excelWorksheet1.Cells.Item[i,5]),
StrToFloat(excelWorksheet1.Cells.Item[i,6]),
StrToFloat(excelWorksheet1.Cells.Item[i,7]),
StrToFloat(excelWorksheet1.Cells.Item[i,8]),
StrToFloat(excelWorksheet1.Cells.Item[i,9]),
StrToFloat(excelWorksheet1.Cells.Item[i,10]),
StrToFloat(excelWorksheet1.Cells.Item[i,11]),
StrToFloat(excelWorksheet1.Cells.Item[i,12]),
StrToFloat(excelWorksheet1.Cells.Item[i,13]),
StrToFloat(excelWorksheet1.Cells.Item[i,14]),
StrToFloat(excelWorksheet1.Cells.Item[i,15]),
StrToFloat(excelWorksheet1.Cells.Item[i,16])) then
begin
if line='' then
begin
line:=Inttostr(i);
end else
begin
line:=','+Inttostr(i);
end;
err_cod1:='数据格式出错在第:'+inttostr(i)+'行.';
exit;
end;
end;
Inc(i);
end;
except //on E:Exception do
MessageBox(stockjc_6.Handle,PChar('数据格式出错在第:'+inttostr(i)+'行.'),'错误(部品)A',64); //<span style="color: #FF0000;">就是它没有show出来</span>.
Exit;
end;
finally
if line<>'' then
begin
messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64)
end;
end;
procedure tstockjc_6.ExcelToSql(filename:string); //部品导入
var
excelApplication1:TExcelApplication;
excelWorksheet1:TExcelWorksheet;
excelWorkbook1:TExcelWorkbook;
i,j:Integer;
line:string;
begin
err_cod1:='';
line:='';
try
excelApplication1:= TExcelApplication.Create(Application);
excelWorksheet1:=TExcelWorksheet.Create(Application);
excelWorkbook1:=TExcelWorkbook.Create(Application);
except
Application.MessageBox('没有安装excel!','错误',MB_ICONERROR+mb_Ok) ;
Exit;
end;
try
excelWorkbook1.ConnectTo(excelApplication1.Workbooks.Open(filename,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,0));
excelWorkbook1.ConnectTo(excelApplication1.Workbooks[1]);
excelWorksheet1.ConnectTo(excelWorkbook1.Worksheets[1] as _worksheet);
except
MessageBox(stockjc_6.Handle,'打开Excel出错.','错误',64);
Exit;
end;
i:=2;
//j:=excelApplication1.Rows.Count;
j:=excelWorksheet1.UsedRange[65536].Rows.Count+1;
if j<2 then
begin
ShowMessage('表格数据为空!');
Exit;
end;
try
// try
while i<=j do
begin
if Trim(excelWorksheet1.Cells.Item[i,2])<>'' then
begin
if not AddtoSQL(excelWorksheet1.Cells.Item[i,1], //这个地方会报错,因为数据格式不下确.
Trim(excelWorksheet1.Cells.Item[i,2]),
trim(excelWorksheet1.Cells.Item[i,3]),
Trim(excelWorksheet1.Cells.Item[i,4]),
StrToFloat(excelWorksheet1.Cells.Item[i,5]),
StrToFloat(excelWorksheet1.Cells.Item[i,6]),
StrToFloat(excelWorksheet1.Cells.Item[i,7]),
StrToFloat(excelWorksheet1.Cells.Item[i,8]),
StrToFloat(excelWorksheet1.Cells.Item[i,9]),
StrToFloat(excelWorksheet1.Cells.Item[i,10]),
StrToFloat(excelWorksheet1.Cells.Item[i,11]),
StrToFloat(excelWorksheet1.Cells.Item[i,12]),
StrToFloat(excelWorksheet1.Cells.Item[i,13]),
StrToFloat(excelWorksheet1.Cells.Item[i,14]),
StrToFloat(excelWorksheet1.Cells.Item[i,15]),
StrToFloat(excelWorksheet1.Cells.Item[i,16])) then
begin
if line='' then
begin
line:=Inttostr(i);
end else
begin
line:=','+Inttostr(i);
end;
err_cod1:='数据格式出错在第:'+inttostr(i)+'行.';
exit;
end;
end;
Inc(i);
end;
except //on E:Exception do
MessageBox(stockjc_6.Handle,PChar('数据格式出错在第:'+inttostr(i)+'行.'),'错误(部品)A',64); //就是它没有show出来.
Exit;
end;
finally
if line<>'' then
begin
messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64)
end;
end;
try
excelApplication1.Disconnect;
excelApplication1.Quit;
excelApplication1.Free;
excelWorksheet1.Free;
excelWorkbook1.Free;
except
ShowMessage('关闭文件出错!');
end;
//调用函数.
procedure Tstockjc_6.N5Click(Sender: TObject);
var
dialog:TOpenDialog;
sql,filename:string;
begin
try
with ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('delete bp_pd');
ExecSQL;
end;
dialog:=TOpenDialog.Create(Self);
dialog.FileName:='*.xls';
dialog.Filter:='*.xls';
if not dialog.Execute then Exit;
filename:=dialog.FileName;
ExcelToSql(filename);
with ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('update bp_pd set zsl=cksl');
ExecSQL;
end;
with ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('select * from bp_pd order by bppf');
Open;
end;
MessageBox(handle,'打开浏览数据完成.','提示',64);
except
MessageBox(handle,PChar('数据出错.'),'错误(部品)B',16); //这里就有显示.
Exit;
end;