匯入匯出資料需要元件:opendialog(開啟選擇檔案視窗),uses中需要引入comobj(提供createoleobject方法進行com程式設計
思路:找到需要匯入的資料**,依次遍歷每一行的資料,獲取到每一行物件的每個屬性值,依次插入到資料庫中。
**如下:
**如下:procedure tform1.toolbutton6click(sender: tobject);
const
//初始行,選擇excel**中資料的哪行開始插入
beginrow=2;
//初始列,選擇excel**中資料的哪列開始插入
beginclo=1;
var //儲存excel當前行,當前列
irow,icol:integer;
//設定變體型別:資料型別只有在執行時候知道
m***cel,m***celworkbook:variant;
//儲存插入語句
sql1:string;
//儲存excel中每一行物件的各個屬性值
a,b,c,d,e:string;
begin
try //如果開啟檔案元件沒有執行,則退出
if not opendialog1.execute then
begin
exit;
end;
//extractfileext獲取檔案字尾的函式
//如果開啟檔案的型別不是**,則提示
if extractfileext(opendialog1.filename)<> '.xlsx' then
begin
//messagebox訊息框控制項
messagebox(0,'請選擇正確的excel檔案',pchar('提示'),mb_ok or mb_iconwarning);
exit;
end;
//建立**物件(createoleobject建立ole物件的函式,com(元件物件模型)程式設計的基礎)
//**顯示
m***cel.visible:= true;
//開啟檔案
m***celworkbook := m***cel.workbooks.open(opendialog1.filename);
except
exit;
end;
try //儲存初始值
irow := beginrow;
icol := beginclo;
//當單元格值不為空時,迴圈遍歷每個單元格的值
//trim將字串前後的空白及控制字元清掉
while trim(m***cel.worksheets['sheet1'].cells[irow,icol].value)<> '' do
begin
a := trim(m***cel.worksheets[1].cells[irow,icol].value);// 序號
b := trim(m***cel.worksheets[1].cells[irow,icol+1].value);// 學號
c := trim(m***cel.worksheets[1].cells[irow,icol+2].value);// 姓名
d := trim(m***cel.worksheets[1].cells[irow,icol+3].value);// 年齡
e := trim(m***cel.worksheets[1].cells[irow,icol+4].value);// 聯絡**
//插入語句
sql1 :='insert into student(id,sno,name,age,telephone) values';
sql1 := sql1 +' ('''+a+''','''+b+''','''+c+''','''+d+''','''+e+''')';
showmessage(sql1);
//插入操作
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add(sql1);
adoquery1.execsql;
//行增加
irow:=irow+1;
end;
//迴圈結束,插入完成,退出
m***cel.quit;
except
//如果有異常,則提示失敗
messagebox(self.handle,'資料匯入失敗!','系統提示',0);
m***cel.quit;
exit;
end;
//否則資料匯入成功
messagebox(self.handle,'資料匯入成功!','系統提示',0);
end;
procedure tform1.toolbutton7click(sender: tobject);
var //插入行
i: integer;
// 設定變體型別,只有在執行期間才能知道型別
excelversion: integer;
begin
//設定匯出文件名
opendialog1.filename:='匯出學生資訊.xlsx';
if opendialog1.execute then
try //建立**物件
//不顯示
//**中新增工作本
//設定**中一些相關屬性
//設定單元格型別為文字
//設定列長度
i:=2;
//迴圈遍歷匯出資料,將adoquery1中的資料集賦值給每個單元格
while not adoquery1.eof do
begin
i:=i+1;
adoquery1.next;
end;
//如果**已經存在,則刪除,重新匯入
if fileexists(opendialog1.filename) then
deletefile(pchar(opendialog1.filename));
//儲存**工作簿
except
messagebox(self.handle,'資料匯出失敗!','系統提示',0);
end;
messagebox(self.handle,'資料匯出成功!','系統提示',0);
end;
Delphi6函式大全 SysUtils pas
首部 function languages tlanguages sysutils.pas 功能 返回系統語言物件 說明 通過此函式可以得到系統的語言環境 參考 type sysutils.tlanguages 例子 begin languages procedure tform1.button1c...
怎樣在C 中呼叫Delphi6寫的DLL
我在編寫乙個系統時遇到了乙個問題,無法在c 中呼叫delphi6寫的dll,只因為dll的引數是string型別的。然後在網上找相關的資料,還是沒有結果。經過我的再三琢磨,現在已經解決,特寫此文章與大家分享我的喜愉!dellphi dll檔案 library mydll uses sysutils,...
Poi簡單實現Excel的匯出匯入功能
最近專案用到了有關excel的匯入匯出功能,之前也沒有使用過,簡單的寫一下基本用法 小白階段有寫錯的還望大神們指教,小弟先謝過了!步驟思想 匯出excel 1.建立乙個工作簿workbook 2.建立乙個sheet 3.建立row 4.為每一行 row 的cell賦值 example string ...