以下为引用的内容:
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