码迷,mamicode.com
首页 > 数据库 > 详细

uniDBGrid导入数据库(转红鱼儿)

时间:2016-01-31 13:17:11      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

有朋友问如何将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导入数据库(转红鱼儿)

标签:

原文地址:http://www.cnblogs.com/xalion/p/5172856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!