2012-08-22 230 views
0

我正在使用SQLTools 1.5編寫Oracle SQL腳本。我想從Excel文件導入數據到Oracle數據庫。我該怎麼做?將數據導入或導出到Excel到Oracle數據庫

另外如何將數據從Oracle數據庫導出到Excel文件?

+1

坦率地說,你可能會發現,最簡單的寫一個Excel VBA腳本(或與Excel相同的PC上運行的獨立VB.Net程序),並從Oracle導入。 微軟/ PC客戶端工具通常非常容易使用。恕我直言...... – paulsm4

+0

@ Paulsm4,謝謝你的建議。但我對VBA或VB.Net程序知之甚少。 – sam

+0

相信我 - 在使用MS Excel或MS VB.Net工作的努力與使用SQLTools之間可能存在一定數量級的差異。恕我直言... – paulsm4

回答

1

SQL Tools支持從Excel文件導入數據到Oracle。您可以使用Oracle的SQL Developer將數據從Excel文件導入數據庫。

傑夫·史密斯解釋了他的blog post你如何能做到這樣

  • 右鍵點擊表 - >導入數據
  • 選擇您的文件
  • 精神頭!你的Excel文件是否有列標題?我們是否想把這些看作是一排桌子?可能不會。默認 選項照顧這一點。
  • 選擇要導入的Excel列
  • 告訴SQL Developer電子表格中的哪些列與Oracle表中的列匹配。
  • 點擊'驗證'按鈕。修復任何錯誤。
  • 一切都看起來不錯!點擊'完成'按鈕。
  • 驗證導入看一看你的新表中的數據

你可以從SQL Developer中反向爲好,只要右擊表&選擇導出。

0
Below sniplet works... 

Imports Oracle.DataAccess.Client 
Imports System.Configuration 
Imports System.IO 
Imports Excel = Microsoft.Office.Interop.Excel 

Module Module1 

    Sub Main() 
     Dim dt As New DataTable 
     Dim connString = ConfigurationManager.ConnectionStrings("oraConnectionString").ConnectionString 
     Using conn As New OracleConnection(connString) 
      Dim cmd As New OracleCommand 
      cmd.Connection = conn 
      cmd.CommandText = "select Doc_name,Doc_id from Document;" 
      conn.Open() 
      Using oda As New OracleDataAdapter(cmd.CommandText, conn) 
       oda.Fill(dt) 
      End Using 
      conn.Close() 
     End Using 
     'exportTOExcel 
     savetoXcel(dt, dt.Rows.Count) 

    End Sub 

    Private Sub savetoXcel(dt As DataTable, rows As Int64) 
     Dim appXL As Excel.Application 
     Dim wbXl As Excel.Workbook 
     Dim shXL As Excel.Worksheet 
     Dim raXL As Excel.Range 
     ' Start Excel and get Application object. 
     appXL = CreateObject("Excel.Application") 
     appXL.Visible = True 
     ' Add a new workbook. 
     wbXl = appXL.Workbooks.Add 
     shXL = wbXl.ActiveSheet 
     ' Add table headers going cell by cell. 
     shXL.Cells(1, 1).Value = "Document ID" 
     shXL.Cells(1, 2).Value = "Document Name" 
     Console.WriteLine("rows:" + rows.ToString) 
     ' Format A1:D1 as bold, vertical alignment = center. 
     With shXL.Range("A1", "B1") 
      .Font.Bold = True 
      .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter 
     End With 
     ' Create an array to set multiple values at once. 
     Dim students(rows, 2) As String 
     Dim i As Int32 = 0 
     For Each Dr As DataRow In dt.Rows 
      students(i, 0) = Dr.Item(0) 
      students(i, 1) = Dr.Item(1) 
      i = i + 1 
     Next 

     Dim BRange As String = "B" + rows.ToString 
     'Console.WriteLine("BRange:" + BRange) 
     shXL.Range("A2", BRange).Value = students 

     'AutoFit columns A:D. 
     raXL = shXL.Range("A1", "B1") 
     raXL.EntireColumn.AutoFit() 
     ' Make sure Excel is visible and give the user control 
     ' of Excel's lifetime. 
     appXL.Visible = True 
     appXL.UserControl = True 
     'Export to excel 
     wbXl.SaveAs(Filename:="d:\Db_report.xls", FileFormat:=50, ReadOnlyRecommended:=True) 
     ' Release object references. 
     raXL = Nothing 
     shXL = Nothing 
     wbXl = Nothing 
     appXL.Quit() 
     appXL = Nothing 
     Exit Sub 

    End Sub 
End Module