用delphi將EXCEL導入數據庫三


單元接口部分引用 comobj 單元(uses )
procedure TForm1.Button1Click(Sender: TObject);

var excelx,excely : string;
  
begin

    try

      ExcelApp := CreateOleObject('Excel.Application');

      WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog對話框指定
//excel檔路徑

                                             

      ExcelApp.Visible := false;

      ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;

      for i := 1 to excelrowcount + 1 do

      begin
        
        excelx := excelapp.Cells[i,1].Value;

        excely := excelapp.Cells[i,2].Value;

        if ((excelapp.Cells[i,1].Value  = '') and (ExcelApp.Cells[i,2].Value = '')) then 
//指定excel檔的第 i 行 ,第 1,2(看情況而定)行如果為空就退出,這樣的設定,最好是你的檔案力這兩行//對應數據庫中不能為空的數據

          exit

        else

        with query1 do

        begin

          close;
          sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel檔的第一列插入到test表的 name欄位;
parambyname('address').asstring := excely;//excel檔的第二列插入到test表的 address 欄位;
execsql;
 
        end;
  
      end;

      finally

        WorkBook.Close;

        ExcelApp.Quit;

        ExcelApp := Unassigned;

        WorkBook := Unassigned;

    end;

  end;
8
| 評論(2)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM