zoukankan      html  css  js  c++  java
  • Delphi Excel导入 的通用程序 .


    Delphi Excel导入 的通用程序 .
    分类: delphi 2012-09-24 18:19 127人阅读 评论(0) 收藏 举报
    exceldelphiintegerprocedure TForm1.btnClick(Sender: TObject);
    begin
      OpenDialog1.Title := '请选择正确的excel文件';
      OpenDialog1.Filter := 'Excel(*.xls)|*.xls';
      if OpenDialog1.Execute then
      edit1.Text := OpenDialog1.FileName;
    end;


    procedure TForm1.btninClick(Sender: TObject);
    const
       
      BeginRow = 2; BeginCol = 1;
    var
      Excel: OleVariant;
      iRow,iCol : integer;
      xlsFilename: string;
    begin
    if (trim(edit1.Text) = '') then
      begin
      MessageBox(GetActiveWindow(), 请选择正确的excel路径', MB_OK +
      MB_ICONWARNING);
      exit;
      end;
      xlsFilename := trim(edit1.Text);
      try
      Excel := CreateOLEObject('Excel.Application');
      except
      Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
      Exit;
      end;
      Excel.Visible := false;
      Excel.WorkBooks.Open(xlsFilename);
      try
      iRow := BeginRow;
      iCol := BeginCol;
       
      while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do begin
      with ADOQuery1 do begin
      Append;
      Fields[0].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
      Fields[1].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol+1].value);
      Fields[2].Asstring := trim(Excel.WorkSheets[1].Cells[iRow,iCol+2].value);
      iRow := iRow + 1;
      end;
       
      end;
      Excel.Quit;
      ADOQuery1.UpdateStatus ;
      except
      Application.MessageBox('导入数据出错', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
      Excel.Quit;
      end;
      MessageBox(GetActiveWindow(), '数据导入成功', '提示信息', MB_OK +
      MB_ICONWARNING);
    end;

  • 相关阅读:
    likely(x)与unlikely(x) __builtin_expect
    c++ ScopeExitGuard
    c++ 11 bind function
    boost shared_ptr weak_ptr
    PL/SQL:使用pragma restrict_references限制包权限
    Nginx的事件处理机制
    读书笔记-HBase in Action-第二部分Advanced concepts-(1)HBase table design
    谈谈工作
    [Java 8] (5) 使用Lambda表达式进行设计
    How to delete a large number of data in SharePoint for List when refreshing data?
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/3096742.html
Copyright © 2011-2022 走看看