简体   繁体   English

将数据导入Excel或从Excel导入Oracle数据库

[英]Importing or exporting data to & from Excel to Oracle database

I am using SQLTools 1.5 for writing Oracle SQL scripts. 我正在使用SQLTools 1.5编写Oracle SQL脚本。 I want to import data from Excel file to Oracle database. 我想将数据从Excel文件导入Oracle数据库。 How can I do it? 我该怎么做?

Also how can I export data from Oracle database to Excel file? 另外,如何将数据从Oracle数据库导出到Excel文件?

SQL Tools supports importing data from Excel file to Oracle. SQL工具支持将数据从Excel文件导入到Oracle。 You could use Oracle's SQL Developer to import data to your database from Excel file. 您可以使用Oracle的SQL Developer从Excel文件将数据导入数据库。

Jeff Smith explains in his blog post how you can do so 杰夫·史密斯(Jeff Smith)在博客中解释了如何做到这一点

  • Right click on Table –> Import Data 右键单击表格–>导入数据
  • Select your file 选择你的文件
  • Mind the headers! 注意标题! Does your Excel file have column headers? 您的Excel文件是否具有列标题? Do we want to treat those as a row to the table? 我们是否要将它们视为表的一行? Probably not. 可能不是。 The default options take care of this. 默认选项可以解决此问题。
  • Choose the Excel columns to be imported 选择要导入的Excel列
  • Tell SQL Developer what columns in the spreadsheet match up to what columns in the Oracle table. 告诉SQL Developer电子表格中的哪些列与Oracle表中的哪些列匹配。
  • Hit the 'verify' button. 点击“验证”按钮。 Fix any mistakes. 修正任何错误。
  • Everything looks right! 一切看起来都不错! Click on the 'Finish' button. 点击“完成”按钮。
  • Verify the import look at your new table data 验证导入是否符合新表数据

You can do the reverse from SQL Developer as well, just right click the table & select Export. 您也可以从SQL Developer中进行相反的操作,只需右键单击表并选择Export。

check this, it may help 检查一下,可能有帮助

WebUtil: How to Read an Excel file into an Oracle Form WebUtil:如何将Excel文件读入Oracle表单

https://sites.google.com/site/craigsoraclestuff/oracle-forms-webutil/read-excel-into-forms https://sites.google.com/site/craigsoraclestuff/oracle-forms-webutil/read-excel-into-forms

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM