uniDBGrid导入数据库(转红鱼儿)
时间:2021-07-01 10:21:17
帮助过:2人阅读
有朋友问如何将excel导入数据库,这是我做的uniGUI项目中代码,实现uniDBGrid导入数据库的函数,因为用了kbmMW,所以你看到是将uniDBGrid导入kbmMWClientQuery,然后提交的ClientQuery到数据库。共享了!
function ImportFromFile(aDataSet: TkbmMWClientQuery; aGrid:
TUniDBGrid; aFileName: string): Boolean;
var
r, c, i: integer;
// v: TCellValue;
xls: TExcelfile;
begin
aDataSet.DisableControls;
xls := TXlsFile.Create(False);
try
xls.Open(aFileName);
xls.ActiveSheet := 1;
for r := 2
to xls.RowCount do
begin
if not IsBlankRow(xls, r) then // 不是空行则填加
begin
aDataSet.Append;
for i := 0 to aGrid.Columns.Count - 1 do
begin
c := GetColByName(xls, aGrid.Columns[i].Title.Caption);
if c <> -1 then
begin
case aDataSet.FieldByName(aGrid.Columns[i].FieldName).DataType
of
ftDateTime, ftDate:
aDataSet.FieldByName(aGrid.Columns[i].FieldName).AsDateTime :=
xls.GetCellValue(r, c).ToDateTime(False);
else
aDataSet.FieldByName(aGrid.Columns[i].FieldName).AsString :=
xls.GetCellValue(r, c)
.ToString.Replace(‘ ‘, ‘‘).Replace(‘‘‘‘, ‘‘);
end;
end;
end;
aDataSet.Post;
end;
end;
result :=
aDataSet.Resolve;
finally
aDataSet.EnableControls;
xls.Free;
end;
end;
uniDBGrid导入数据库(转红鱼儿)
标签: