VB.NET 使用ADODB連接資料庫滙出到EXCEL

前端之家收集整理的这篇文章主要介绍了VB.NET 使用ADODB連接資料庫滙出到EXCEL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

‘導入命名空間

Imports ADODB

Imports Microsoft.Office.Interop

 

Private Sub A1()

Dim sql As String
Dim Cnn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim S As String

 

S = "Provider=OraOLEDB.Oracle.1;Password=密碼;Persist Security Info=True;User ID=用戶名;Data Source=連線位置"
Cnn.Open(S)
Rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient

sql = "SELECT IMG_FILE.IMG01 FROM SH01.IMG_FILE"
Rs.Open(sql,Cnn,3,3)
Dim Xls As Excel.Application = New Excel.Application ‘定义excel应用程序
Dim Xlsbook As Excel.Workbook ‘定义工作簿
Dim Xlssheet As Excel.Worksheet ‘定义工作表
Xlsbook = Xls.Application.Workbooks.Add ‘添加新工作簿
Xlssheet = Xlsbook.Sheets(1) ‘第1个工作表的控制句柄
Xls.Visible = True ‘显示excel 程序

Xlssheet.Cells(1,1).value = "表頭1"

Xlssheet.Range("a2").CopyFromRecordset(Rs)
Xlssheet.Cells.EntireColumn.AutoFit()
Xls = Nothing ‘释放EXCEL对象
Rs.Cancel()

End Sub

猜你在找的VB相关文章