Delphi 加载Excel 导入数据库


本文整理自网络,侵删。

 
uses  ComObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  ESatir = $0000000B;
var
  Adet: Integer;
  Book: Variant;
  SSay, j, I: Integer;
  Excel, Sheet: Variant;
begin
  Adet := 0;

  OpenDialog.Filter := 'Excel *.xls,*.xlsx|*.xls;*.xlsx';
  OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect];
  if OpenDialog.Execute then
  begin
    for I := 0 to OpenDialog.Files.Count - 1 do
    begin
      Excel := CreateOleObject('Excel.Application');
      Book := Excel.WorkBooks.Open(OpenDialog.Files[I]);
      Sheet := Book.WorkSheets['Master']; // Sayfa ad?
      SSay := Excel.ActiveSheet.Cells.SpecialCells(ESatir, EmptyParam).Row;
      try
        With FDQuery do
        begin
          for j := 5 to SSay do
          begin
            Inc(Adet);
            Close;
            SQL.Clear;
            SQL.Add('Insert Into LIST (SIRANO, GIRIS, IDADI, URUN)');
            SQL.Add(' Values (:SR, :GR, :IA, :UR)');
            ParamByName('SR').AsInteger := Adet;
            ParamByName('GR').Value := Sheet.Cells[j, 2].Text;
            ParamByName('IA').Value := Sheet.Cells[j, 3].Text;
            ParamByName('UR').Value := Sheet.Cells[j, 4].Text;
            ExecSQL;
Application.ProcessMessages;
          end;
        end;
      finally
        Screen.Cursor := crDefault;
        Excel.WorkBooks.Close;
        Excel.Quit;
        Excel := Unassigned;
        Sheet := Unassigned;
      end;
    end;
  end;
  FDQuery.Close;

end;

end;

相关阅读 >>

Delphi串口api函数

Delphi中实现变长函数笔记

Delphi使用tclientdataset时不携带midas.dll的方法

Delphi tnotification android通知

老外写的在桌面添加快捷方式(Delphi xe5 android)

Delphi closehandle的解释

Delphi 获取网卡信息

Delphi版 随机窗口类名

idhttp相关:伪造来源地址、cookie欺骗、通过代理访问

Delphi 验证文件签名

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...