把EXCEL的資料匯入資料庫(ACCESS、MSSQL等)

來源:互聯網
上載者:User

 

以下代碼出自http://blog.csdn.net/hellogv/,引用請註明出處!

首先,本文面向有資料庫開發經驗的朋友。如果你自認“功力深厚”或者覺得本文對你沒有什麼協助,那麼請不要見笑,以下小弟寫的代碼。

在把EXCEL的資料匯入資料庫之前,先進行一些簡單的準備工作:

1。把資料所在的EXCEL表另儲存為DBF 4格式。

2。開啟BCB,添加AdoTable(改名為DBFTable)和DataSource這兩個控制項

OK,準備工作,到此結束,剩下的就是打代碼了

1。在Form_Load()事件中,加入以下代碼:

AnsiString filepath=ExtractFilePath(FileName); //FileName為DBF檔案名稱 AnsiString tablename=ExtractFileName(FileName).SubString(0,ExtractFileName(FileName).Length()-4);//去除檔案的副檔名

//--------------------用於串連DBF檔案--------------------------------    DBFTable->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Password=/"/";Data Source=/""+filepath+"/";Extended Properties=dbase 5.0;Persist Security Info=True";    DBFTable->TableName=tablename;    DBFTable->Open();

2。建立一個類的成員函數void __fastcall ExcelToDB(TADOQuery *ADOQuery);

以下是函數的具體實現:

void __fastcall TDBFToDBForm::ExcelToDB(TADOQuery *ADOQuery) {//實現很簡單,只是把DBFTable的資料逐項添加的本地的資料庫     DBFTable->First();    for(int j=0;j<DBFTable->RecordCount;j++)    {      ADOQuery->Append();      for(int i=0;i<DBFTable->FieldCount;i++)      ADOQuery->FieldByName(DBFTable->FieldList->Strings[i])->AsString=DBFTable->FieldByName(DBFTable->FieldList->Strings[i])->AsString;      ADOQuery->Post();      DBFTable->Next();     } ShowMessage("匯入成功!"); }

說明:DBFTable使用來裝載EXCEL另儲存的資料,而程式本身裝載資料用的是ADOQuery(當然,你也可以改變),

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.