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.NET 使用ADODB連接資料庫滙出到EXCEL