我的实现代码如下:不懂的地方请提出!procedure TPPms010.LoadExcelFile(FileName:string);
var
i,j,rows:integer;
ExcelApp, WorkBook, WorkSheet : Variant;
begin
inherited;
cols:=0;
try
ExcelApp := CreateOleObject('Excel.Application');
except
on E:EolesysError do
begin
DispErr(E.Message);
abort;
end
else
begin
DispErr('PPMS010-02 Could not Create Excel Application!');
abort;
end;
end;
try
try
ExcelApp.WorkBooks.Open(edtDir.text,,True);
WorkBook:=ExcelApp.ActiveWorkbook;
WorkSheet:=WorkBook.ActiveSheet;
except
DispInf('PPMS010-01 Can''t open file!', MB_OK + MB_ICONINFORMATION);
abort;
end;
dmppms010.tblAm_allocation_Head.Append ;
SetHeadValues; // set master table field value
rows:=GetSheetRows(WorkSheet);
PrgrsBar.Max:=rows;
PrgrsBar.step:=1;
PrgrsBar.Show;
for i:=1 to rows do
begin
dmppms010.tblAm_Allocation_Line.Append;
SetLineValues; //将excel该行数据赋给detail表的各字段
PrgrsBar.StepIt ;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp:= unassigned;
PrgrsBar.Hide ;
end;
end;procedure TPPms010.Save;
begin
with dmPpms010.tblAm_allocation_Head do
if not Database.InTransaction then
Database.StartTransaction ;
try
with dmPpms010.tblAm_allocation_Head do
if Modified or UpdatesPending then //modified 为一公用变量,表示表数据是否改动过!
ApplyUpdates;
with dmPpms010.tblAm_allocation_Line do
if Modified or UpdatesPending then
ApplyUpdates;
dmPpms010.tblAm_allocation_Line.Database.Commit ;
dmPpms010.tblAm_allocation_Line.Database.StartTransaction ;
dmPPms010.Modified :=false;
sptnSave.Enabled :=false;
except
on E:EDBEngineError do
begin
dmPpms010.tblAm_allocation_Head.Database.Rollback ;
Ppms010.ShowError(E);
end;
end;
end;
var
i,j,rows:integer;
ExcelApp, WorkBook, WorkSheet : Variant;
begin
inherited;
cols:=0;
try
ExcelApp := CreateOleObject('Excel.Application');
except
on E:EolesysError do
begin
DispErr(E.Message);
abort;
end
else
begin
DispErr('PPMS010-02 Could not Create Excel Application!');
abort;
end;
end;
try
try
ExcelApp.WorkBooks.Open(edtDir.text,,True);
WorkBook:=ExcelApp.ActiveWorkbook;
WorkSheet:=WorkBook.ActiveSheet;
except
DispInf('PPMS010-01 Can''t open file!', MB_OK + MB_ICONINFORMATION);
abort;
end;
dmppms010.tblAm_allocation_Head.Append ;
SetHeadValues; // set master table field value
rows:=GetSheetRows(WorkSheet);
PrgrsBar.Max:=rows;
PrgrsBar.step:=1;
PrgrsBar.Show;
for i:=1 to rows do
begin
dmppms010.tblAm_Allocation_Line.Append;
SetLineValues; //将excel该行数据赋给detail表的各字段
PrgrsBar.StepIt ;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp:= unassigned;
PrgrsBar.Hide ;
end;
end;procedure TPPms010.Save;
begin
with dmPpms010.tblAm_allocation_Head do
if not Database.InTransaction then
Database.StartTransaction ;
try
with dmPpms010.tblAm_allocation_Head do
if Modified or UpdatesPending then //modified 为一公用变量,表示表数据是否改动过!
ApplyUpdates;
with dmPpms010.tblAm_allocation_Line do
if Modified or UpdatesPending then
ApplyUpdates;
dmPpms010.tblAm_allocation_Line.Database.Commit ;
dmPpms010.tblAm_allocation_Line.Database.StartTransaction ;
dmPPms010.Modified :=false;
sptnSave.Enabled :=false;
except
on E:EDBEngineError do
begin
dmPpms010.tblAm_allocation_Head.Database.Rollback ;
Ppms010.ShowError(E);
end;
end;
end;
所以我又有了一个建议:
在table的onposterror事件中将出错的记录以一种格式导出到一个文本文件中,然后继续导下一条记录,导完后,再对那个文本文件中的数据进行编辑,然后再以刚才你在文本文件中认定的那种格式将文本文件中的数据导入数据库
我在CachedUpdate过程中提交多条数据,怎么样才不中断update忽略错误的一行数据而让其他的数据能够顺利提交?
我在那设置断点,还没有到那就已经有错误了!
是applyupdates过程引起的!
我怎么去查?关键是我已经导入的数据还在本地缓存中未提交!怎么查询这里面有没有重复记录!
不会叫我去遍历EXCEL表查吧?
在windows中我怎么查错?
procedure Table1postError(DataSet: TDataSet;
E: EDatabaseError; var Action: TDataAction);
begin
if (e is EDBEngineError) then
MyErrMessage((e as EDBEngineError).Errors[0].ErrorCode,'自定义的错误信息');
end;procedure MyErrMessage(errid:integer;errmessage:string);
var em:string;
begin
///////////
case errid of
9729:em:='内容不能为空!';
9730:em:='输入的数值过小!' ;
9731:em:='输入的数值过大!' ;
9732:em:='内容不能为空111!' ;
9740:em:='内容不能为空2222!' ;
else ;
em:=errmessage; //显示自定义的错误信息
end;
showmessage(em);
abort;
///////////
end;
E: EDatabaseError; var Action: TDataAction);
begin
if (e is EDBEngineError) then
//在这里将刚才读到的信息保存到文本文件中
//加入写文件文件的代码
abort;
end;
我先去吃饭了!交个朋友? [email protected]