如何将表中的数据导出到电子表格中
如何將表中的數(shù)據(jù)導(dǎo)出到電子表格中 ?
? ?
? ?
? ?
? ?
? 有很多方法都可將數(shù)據(jù)庫(kù)中某個(gè)表的數(shù)據(jù)導(dǎo)出到電子表格中,例如通過(guò)創(chuàng)建Access.Application,可以利用Access本身的導(dǎo)出功能實(shí)現(xiàn)將表中的數(shù)據(jù)導(dǎo)出到電子表格中。但是這種方法會(huì)占用較多的系統(tǒng)資源,并且缺乏通用性。如果一個(gè)數(shù)據(jù)庫(kù)沒(méi)有導(dǎo)出的功能怎么辦?下面的這段程序代碼利用記錄集實(shí)現(xiàn)導(dǎo)出的功能,這種做法的好處是顯而易見(jiàn)的:你可以控制要導(dǎo)出的數(shù)據(jù),而不用將整個(gè)表的內(nèi)容都導(dǎo)出到電子表格中。為簡(jiǎn)單起見(jiàn)下面的程序代碼仍將整個(gè)表的數(shù)據(jù)導(dǎo)出到電子表格中。如果你有興趣的話,對(duì)下面的代碼稍加改動(dòng)就可做成更為通用的一個(gè)類或是一個(gè)控件。 ? ?
? ?
? 首先在窗體上添加一個(gè)標(biāo)簽控件和一個(gè)命令按鈕,然后在工程中添加對(duì)DAO引用。利用下面的程序代碼就可將表中的數(shù)據(jù)導(dǎo)出到電子表格中。 ?
? ?
? Option ? Explicit ?
? ?
? Private ? Sub ? Command1_Click() ?
? ? ? ? ? ? ? ? ? Dim ? tempDB ? As ? Database ?
? ? ? ? ? ? ? ? ? Dim ? i ? As ? Integer ? ? ? ? ? ? ? ? ? ? ? ' ? 循環(huán)計(jì)數(shù)器 ?
? ? ? ? ? ? ? ? ? Dim ? j ? As ? Integer ?
? ? ? ? ? ? ? ? ? Dim ? rCount ? As ? Long ? ? ? ? ? ? ? ? ? ' ? 記錄的個(gè)數(shù) ?
? ? ? ? ? ? ? ? ? Dim ? xl ? As ? Object ? ? ? ? ? ? ? ? ? ? ? ' ? OLE自動(dòng)化對(duì)象 ?
? ? ? ? ? ? ? ? ? Dim ? Sn ? As ? Recordset ?
? ? ? ? ? ? ? ? ? Screen.MousePointer ? = ? 11 ?
? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "打開(kāi)數(shù)據(jù)庫(kù)..." ?
? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? Set ? tempDB ? = ? Workspaces(0).OpenDatabase("Nwind.mdb") ?
? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "創(chuàng)建Excel對(duì)象..." ?
? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? Set ? xl ? = ? CreateObject("Excel.Sheet.8") ?
? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "創(chuàng)建快照型記錄集..." ?
? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? Set ? Sn ? = ? tempDB.OpenRecordset("Customers", ? dbOpenSnapshot) ?
? ?
? ? ? ? ? ? ? ? ? ? If ? Sn.RecordCount ? > ? 0 ? Then ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "將字段名添加到電子表格中" ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? ? ? ? ? For ? i ? = ? 0 ? To ? Sn.Fields.Count ? - ? 1 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xl.Worksheets(1).cells(1, ? i ? + ? 1).Value ? = ? Sn(i).Name ?
? ? ? ? ? ? ? ? ? ? ? ? ? Next ?
? ? ? ? ? ? ? ? ? ? ? ? ? Sn.MoveLast ?
? ? ? ? ? ? ? ? ? ? ? ? ? Sn.MoveFirst ?
? ? ? ? ? ? ? ? ? ? ? ? ? rCount ? = ? Sn.RecordCount ?
? ? ? ? ? ? ? ? ? ? ? ? ? ' ? 在記錄中循環(huán) ?
? ? ? ? ? ? ? ? ? ? ? ? ? i ? = ? 0 ?
? ? ? ? ? ? ? ? ? ? ? ? ? Do ? While ? Not ? Sn.EOF ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "Record:" ? & ? Str(i ? + ? 1) ? & ? " ? of" ? & ? _ ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Str(rCount) ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? For ? j ? = ? 0 ? To ? Sn.Fields.Count ? - ? 1 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ' ? 加每個(gè)字段的值加到工作表中 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? If ? Sn(j).Type ? < ? 11 ? Then ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xl.Worksheets(1).cells(i ? + ? 2, ? j ? + ? 1).Value ? = ? Sn(j) ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Else ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ' ? 處理Memo和LongBinary ? 類型的字段 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xl.Worksheets(1).cells(i ? + ? 2, ? j ? + ? 1).Value ? = ? "Memo ? or ? Binary ? Data" ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? End ? If ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Next ? j ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Sn.MoveNext ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? i ? = ? i ? + ? 1 ?
? ? ? ? ? ? ? ? ? ? ? ? ? Loop ?
? ? ? ? ? ? ? ? ? ? ? ? ? ' ? 保存工作表 ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "保存文件..." ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? ? ? ? ? xl.SaveAs ? "c:/Customers.XLS" ?
? ? ? ? ? ? ? ? ? ? ? ? ? '從內(nèi)存中刪除Excel對(duì)象 ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "退出Excel" ?
? ? ? ? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? ? ? ? ? xl.Application.Quit ?
? ? ? ? ? ? ? ? ? ? Else ?
? ? ? ? ? ? ? ? ? ? ? ? ? ' ? 沒(méi)有記錄 ?
? ? ? ? ? ? ? ? ? ? End ? If ?
? ? ? ? ? ? ? ? ? ? ' ? 清除 ?
? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "清除對(duì)象" ?
? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ? ? ? Set ? xl ? = ? Nothing ?
? ? ? ? ? ? ? ? ? ? Set ? Sn ? = ? Nothing ?
? ? ? ? ? ? ? ? ? ? Set ? tempDB ? = ? Nothing ?
? ? ? ? ? ? ? ? ? ? Screen.MousePointer ? = ? 0 ? ? ' ? 恢復(fù)鼠標(biāo)指針 ?
? ? ? ? ? ? ? ? ? ? Label1.Caption ? = ? "Ready" ?
? ? ? ? ? ? ? ? ? ? Label1.Refresh ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?
? End ? Sub ?
? ?
? Private ? Sub ? Form_Load() ?
? ? ? ? ? Label1.AutoSize ? = ? True ?
? ? ? ? ? Label1.Caption ? = ? "Ready" ?
? ? ? ? ? Label1.Refresh ?
? End ? Sub ?
? 用DAO打開(kāi)Excel文件 ?
? ?
? _作者:th4005@mail.intonet.net.tw ?
? ?
? ?
? >請(qǐng)問(wèn)不用DATA元件,如何OPEN一個(gè)XLS檔? ?
? >set ? db=OpenDatabase("資料庫(kù)")只能開(kāi)mdb,*.xls檔要如何開(kāi)啟呢? ?
? >請(qǐng)教高手! ?
? ?
? Option ? Explicit ?
? Dim ? Db ? As ? Database ?
? Dim ? Rs ? As ? Recordset ?
? ?
? Private ? Sub ? Form_Load() ?
? Set ? Db ? = ? OpenDatabase("c:/temp/book1.xls", ? False, ? False, ? "Excel ? 8.0;") ?
? Set ? Rs ? = ? Db.OpenRecordset("sheet1$") ?
? End ? Sub ?
? ?
? Private ? Sub ? Form_Unload(Cancel ? As ? Integer) ?
? Rs.Close ?
? Db.Close ?
? End ? Sub ?
? ?
? 這是最簡(jiǎn)單的辦法,詳細(xì)的使用方法及限制請(qǐng)參看 ? VB ? OnLineHelp ? ,資料存取物件手冊(cè)、取得外部資料、使用試算表一章。??
總結(jié)
以上是生活随笔為你收集整理的如何将表中的数据导出到电子表格中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于VB和EXCEL的报表设计及打印
- 下一篇: 还有:用VB创建Excel报表