如何高效的將excel導(dǎo)入到oracle?SqlBulkCopy 導(dǎo)入到sqlserver對應(yīng),oracle也有自身的方法,只是稍微復(fù)雜些.
那就是使用oracle的sql*loader功能,而sqlldr只支持類似csv格式的數(shù)據(jù),所以要自己把excel轉(zhuǎn)換一下。
實現(xiàn)步驟:
用com組件讀取excel-保存為csv格式-處理最后一個字段為null的情況和表頭-根據(jù)excel結(jié)構(gòu)建表-生成sqlldr的控制文件-用sqlldr命令導(dǎo)入數(shù)據(jù)
這個性能雖然沒有sql的bcp快,但還是相當(dāng)可觀的,在我機(jī)器上1萬多數(shù)據(jù)不到4秒,而且導(dǎo)入過程代碼比較簡單,也同樣沒有循環(huán)拼接sql插入那么難以維護(hù)。
這里也提個問題:處理csv文件的表頭和最后一個字段為null的情況是否可以優(yōu)化?
view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
//引用-com-microsoft excel objects 11.0
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///
/// excel導(dǎo)入到oracle
///
/// 文件名
/// sheet名
/// oracle命令sqlplus連接串
public void TransferData(string excelFile, string sheetName, string sqlplusString)
{
string strTempDir = System.IO.Path.GetDirectoryName(excelFile);
string strFileName = System.IO.Path.GetFileNameWithoutExtension(excelFile);
string strCsvPath = strTempDir +"\\"+strFileName + ".csv";
string strCtlPath = strTempDir + "\\" + strFileName + ".Ctl";
string strSqlPath = strTempDir + "\\" + strFileName + ".Sql";
if (System.IO.File.Exists(strCsvPath))
System.IO.File.Delete(strCsvPath);
那就是使用oracle的sql*loader功能,而sqlldr只支持類似csv格式的數(shù)據(jù),所以要自己把excel轉(zhuǎn)換一下。
實現(xiàn)步驟:
用com組件讀取excel-保存為csv格式-處理最后一個字段為null的情況和表頭-根據(jù)excel結(jié)構(gòu)建表-生成sqlldr的控制文件-用sqlldr命令導(dǎo)入數(shù)據(jù)
這個性能雖然沒有sql的bcp快,但還是相當(dāng)可觀的,在我機(jī)器上1萬多數(shù)據(jù)不到4秒,而且導(dǎo)入過程代碼比較簡單,也同樣沒有循環(huán)拼接sql插入那么難以維護(hù)。
這里也提個問題:處理csv文件的表頭和最后一個字段為null的情況是否可以優(yōu)化?
view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
//引用-com-microsoft excel objects 11.0
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///
/// excel導(dǎo)入到oracle
///
/// 文件名
/// sheet名
/// oracle命令sqlplus連接串
public void TransferData(string excelFile, string sheetName, string sqlplusString)
{
string strTempDir = System.IO.Path.GetDirectoryName(excelFile);
string strFileName = System.IO.Path.GetFileNameWithoutExtension(excelFile);
string strCsvPath = strTempDir +"\\"+strFileName + ".csv";
string strCtlPath = strTempDir + "\\" + strFileName + ".Ctl";
string strSqlPath = strTempDir + "\\" + strFileName + ".Sql";
if (System.IO.File.Exists(strCsvPath))
System.IO.File.Delete(strCsvPath);