c#中高效的excel导入oracle的方法

 

如何高效的将excel导入到oracle和前两天的SqlBulkCopy 导入到sqlserver对应,oracle也有自身的方法,只是稍微复杂些.
那就是使用oracle的sql*loader功能,而sqlldr只支持类似csv格式的数据,所以要自己把excel转换一下。

实现步骤:

用com组件读取excel-保存为csv格式-处理最后一个字段为null的情况和表头-根据excel结构建表-生成sqlldr的控制文件-用sqlldr命令导入数据
这个性能虽然没有sql的bcp快,但还是相当可观的,在我机器上1万多数据不到4秒,而且导入过程代码比较简单,也同样没有循环拼接sql插入那么难以维护。

这里也提个问题:处理csv文件的表头和最后一个字段为null的情况是否可以优化除了我代码中的例子,我实在想不出其他办法。

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导入到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);  
 
 
            //获取excel对象  
            Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();  
 
            Microsoft.Office.Interop.Excel.Workbook ObjWorkBook;  
 
            Microsoft.Office.Interop.Excel.Worksheet ObjWorkSheet = null;  
 
            ObjWorkBook = ObjExcel.Workbooks.Open(excelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  
 
            foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in ObjWorkBook.Sheets)  
            {  
                if (sheet.Name.ToLower() == sheetName.ToLower())  
                {  
                    ObjWorkSheet = sheet;  
                    break;  
                }  
            }  
            if (ObjWorkSheet == null) throw new Exception(string.Format("{0} not found!!", sheetName));  
 
 
            //保存为csv临时文件  
            ObjWorkSheet.SaveAs(strCsvPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing, false, false, false, Type.Missing, Type.Missing, false);  
            ObjWorkBook.Close(false, Type.Missing, Type.Missing);  
            ObjExcel.Quit();  
 
            //读取csv文件,需要将表头去掉,并且将最后一列为null的字段处理为显示的null,否则oracle不会识别,这个步骤有没有好的替换方法  
            System.IO.StreamReader reader = new System.IO.StreamReader(strCsvPath,Encoding.GetEncoding("gb2312"));  
            string strAll = reader.ReadToEnd();  
            reader.Close();  
            string strData = strAll.Substring(strAll.IndexOf("/r/n") + 2).Replace(",/r/n",",Null");  
 
            byte[] bytes = System.Text.Encoding.Default.GetBytes(strData);  
            System.IO.Stream ms = System.IO.File.Create(strCsvPath);  
          &n

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值