科技时代新浪首页 > 科技时代 > 学园 > 正文

Visual Basic .NET处理Excel表格全接触(2)


http://www.sina.com.cn 2005年12月16日 12:01 天极yesky

    三.Visual Basic .Net导入数据库数据到Excel表格:

  通过此示例可以方便的把Sql Server数据库中数据表导入到Excel表格中,同样对于其他类型的数据库,如果也需要导入数据到Excel表格,只需要修改此示例中数据连接部分的代码就可以了。以下是在Visual Basic .Net中导入Sql Server数据库到Excel表格的具体的实现步骤:

  1. 启动Visual Studio .Net。

  2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。

  3. 将【项目类型】设置为【Visual Basic项目】。

  4. 将【模板】设置为【Windows应用程序】。

  5. 在【名称】文本框中输入【Visual Basic .Net导入数据库数据到Excel表格】。

  6. 在【位置】的文本框中输入【E:\VS.NET项目】,然后单击【确定】按钮,这样在"E:\VS.NET项目"目录中就产生了名称为"Visual Basic .Net中操作Word文档"的文件夹,并在里面创建了名称为"Visual Basic .Net导入数据库数据到Excel表格"的项目文件。

  7. 把Visual Studio .Net的当前窗口切换到【Form1.vb(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往Form1窗体中拖入下列组件,并执行相应的操作:

  五个Label组件。
  五个TextBox组件。

  一个Button组件,用以实现导入Sql Server数据库到Excel表格,并在这个Button组件拖入Form1的设计窗体后,双击它们,则Visual Stuido .Net会在Form1.vb文件分别产生这组件的Click事件对应的处理代码。

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

VisualBasic.NET处理Excel表格全接触(2)
图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所示:

VisualBasic.NET处理Excel表格全接触(2)
图02:【Visual Basic .Net导入数据库数据到Excel表格】项目的设计界面

  11. 用下列代码替换Form1.vb中的Button1的Click事件对应的处理代码,下列代码功能是根据使用者输入的数据库信息,建立数据连接,打开数据表,并导入到Excel表格中,如果出错则弹出出错信息:

Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click
'创建一个 DataSet
Dim myDataSet As DataSet = New DataSet
'连接数据库,得到数据集
Try
' 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为本地 ,数据库为temp
Dim strCon As String = "Provider = SQLOLEDB.1; Persist Security Info = False; User ID = " & TextBox4.Text & " ;PSW=" & TextBox5.Text _
& " ; Initial Catalog = " & TextBox2.Text & " ;Data Source = " & TextBox1.Text
'数据连接代码,对此修改可导入其他类型数据库到Excle表格
Dim myConn As OleDbConnection = New OleDbConnection ( strCon )
myConn.Open ( )
Dim strCom As String = "SELECT * FROM " & TextBox3.Text
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter ( strCom , myConn )
myCommand.Fill ( myDataSet , "table01" )
'关闭此OleDbConnection
myConn.Close ( )
Catch ey As Exception
MessageBox.Show ( "连接错误! " + ey.ToString ( ) , "错误" )
End Try
Dim table As DataTable = myDataSet.Tables ( "table01" )
'创建一个空的Excel电子表格文档
Dim AppExcel As Excel.Application = New Excel.Application
AppExcel.Application.Workbooks.Add ( True )
'读取数据的字段名称,并在产生的Excel表格的第一行显示出来
Dim colIndex As Integer = 0
Dim col As DataColumn = New DataColumn
For Each col In table.Columns
colIndex = colIndex + 1
AppExcel.Cells ( 1 , colIndex ) = col.ColumnName
Next
'实现数据集到Excel表格的转换
Dim rowIndex As Integer = 1
Dim row As DataRow
For Each row In table.Rows
rowIndex = rowIndex + 1
colIndex = 0
Dim col1 As DataColumn
For Each col1 In table.Columns
colIndex = colIndex + 1
AppExcel.Cells ( rowIndex , colIndex ) = row ( col1.ColumnName ) .ToString ( )
Next
Next
AppExcel.Visible = True
End Sub

  至此在上述步骤都正确完成后,【Visual Basic .Net导入数据库数据到Excel表格】项目的全部工作就基本完成了。此时单击快捷键F5运行程序,在【数据库服务器名】中输入Sql Server数据库所在的机器名称,【数据库名】中输入要打开的数据库名称,【数据表名】中输入要导入到Excel表格的数据表名称,在【用户名】和【口令】中分别输入打开此Sql Server数据库帐户及口令后,单击【导入Excel表格】按钮,则程序会把相应的数据表导入到Excel表格中。图03和图04分别是【Visual Basic .Net导入数据库数据到Excel表格】项目的运行界面和导入数据后的Excel表格:

VisualBasic.NET处理Excel表格全接触(2)
图03:【Visual Basic .Net导入数据库数据到Excel表格】项目的运行界面

VisualBasic.NET处理Excel表格全接触(2)
图04:导入Sql Server数据库数据后形成的Excel表格

  四.总结:

  本文通过一个实用的示例介绍用Visual Basic .Net处理Excel表格的各种典型操作的实现,此示例能够完成从Sql Server数据库导出数据表中的数据到Excel表格。COM组件是一种现成的丰富资源,当.Net FrameWork SDK无法对您解决问题有所帮助时,也许借助COM就是一条捷径。最后希望本文能够对您掌握在Visual Basic .Net中 COM组件的使用方法。有所帮助。

[上一页] [1] [2]

发表评论

爱问(iAsk.com)



评论】【论坛】【收藏此页】【 】【多种方式看新闻】【下载点点通】【打印】【关闭




科技时代意见反馈留言板 电话:010-82628888-5595   欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

Copyright © 1996 - 2005 SINA Inc. All Rights Reserved

版权所有 新浪网