8. 选中【解决方案管理器】,右击【引用】,在弹出的菜单中选中【添加引用】后,按照图01所示,加入要引用的COM组件"Microsoft Excel 9.0 Object Library":

图01:【Visual Basic .Net导入数据库数据到Excel表格】项目【添加引用】对话框
9. 把Visual Studio .Net的当前窗口切换到Form1.vb的代码编辑窗口,并在Form1.vb文件首部添加下列代码,下列代码是导入下面使用的数据类所在的命名空间:
| Imports System.Data.OleDb |
10. 用下列代码替换Form1.vb中的InitializeComponent过程对应的代码,下列代码作用是初始化窗体中加入的组件:
<System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( ) Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.TextBox3 = New System.Windows.Forms.TextBox Me.TextBox5 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.TextBox4 = New System.Windows.Forms.TextBox Me.SuspendLayout ( ) Me.Label1.Location = New System.Drawing.Point ( 40 , 28 ) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size ( 114 , 23 ) Me.Label1.TabIndex = 0 Me.Label1.Text = "数据库服务器名:" Me.Label2.Location = New System.Drawing.Point ( 64 , 59 ) Me.Label2.Name = "Label2" Me.Label2.TabIndex = 1 Me.Label2.Text = "数据库名称:" Me.Label3.Location = New System.Drawing.Point ( 64 , 90 ) Me.Label3.Name = "Label3" Me.Label3.TabIndex = 2 Me.Label3.Text = "数据表名称:" Me.Label4.Location = New System.Drawing.Point ( 88 , 121 ) Me.Label4.Name = "Label4" Me.Label4.TabIndex = 3 Me.Label4.Text = "用户名:" Me.Label5.Location = New System.Drawing.Point ( 98 , 152 ) Me.Label5.Name = "Label5" Me.Label5.TabIndex = 4 Me.Label5.Text = "口令:" Me.TextBox1.Location = New System.Drawing.Point ( 160 , 26 ) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size ( 148 , 21 ) Me.TextBox1.TabIndex = 5 Me.TextBox1.Text = "" Me.TextBox2.Location = New System.Drawing.Point ( 160 , 56 ) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size ( 148 , 21 ) Me.TextBox2.TabIndex = 6 Me.TextBox2.Text = "" Me.TextBox3.Location = New System.Drawing.Point ( 160 , 86 ) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size ( 148 , 21 ) Me.TextBox3.TabIndex = 7 Me.TextBox3.Text = "" Me.TextBox5.Location = New System.Drawing.Point ( 160 , 146 ) Me.TextBox5.Name = "TextBox5" Me.TextBox5.PasswordChar = Microsoft.VisualBasic.ChrW ( 42 ) Me.TextBox5.Size = New System.Drawing.Size ( 148 , 21 ) Me.TextBox5.TabIndex = 9 Me.TextBox5.Text = "" Me.Button1.Location = New System.Drawing.Point ( 120 , 198 ) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size ( 126 , 36 ) Me.Button1.TabIndex = 10 Me.Button1.Text = "导出Excle表格" Me.TextBox4.Location = New System.Drawing.Point ( 160 , 116 ) Me.TextBox4.Name = "TextBox4" Me.TextBox4.Size = New System.Drawing.Size ( 148 , 21 ) Me.TextBox4.TabIndex = 8 Me.TextBox4.Text = "" Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 ) Me.ClientSize = New System.Drawing.Size ( 374 , 271 ) Me.Controls.Add ( Me.Button1 ) Me.Controls.Add ( Me.TextBox5 ) Me.Controls.Add ( Me.TextBox4 ) Me.Controls.Add ( Me.TextBox3 ) Me.Controls.Add ( Me.TextBox2 ) Me.Controls.Add ( Me.TextBox1 ) Me.Controls.Add ( Me.Label5 ) Me.Controls.Add ( Me.Label4 ) Me.Controls.Add ( Me.Label3 ) Me.Controls.Add ( Me.Label2 ) Me.Controls.Add ( Me.Label1 ) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "Visual Basic .Net导入数据库数据到Excel表格" Me.ResumeLayout ( False ) End Sub |
至此【Visual Basic .Net导入数据库数据到Excel表格】项目的界面设计和功能实现的准备工作就完成了,具体如图02所示: