uniDBGrid导入数据库(转红鱼儿)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uniDBGrid导入数据库(转红鱼儿)相关的知识,希望对你有一定的参考价值。
有朋友问如何将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;
// 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导入数据库(转红鱼儿)的主要内容,如果未能解决你的问题,请参考以下文章
UniGUI 如何进行 UniDBGrid 的单元 Cell 的计算 ?