| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> oracle >> c# zhong gao xiao de excel dao ru oracle de fang fa
 

c# zhong gao xiao de excel dao ru oracle de fang fa

2008-07-26 07:42:36 编辑來源:互联网 评论
 
本文为【c#中高效的excel导入oracle的方法】的拼音翻译版
  ru he gao xiao de jiang excel dao ru dao oracle? he qian liang tian de SqlBulkCopy dao ru dao sqlserver dui ying ,oracle ye you zi shen de fang fa , zhi shi shao wei fu duo xie .
  na jiu shi shi yong oracle de sql*loader gong neng , er sqlldr zhi zhi chi lei si csv ge shi de shu ju , suo yi yao zi ji ba excel zhuan huan yi xia 。
  shi xian bu zhou :
  yong com zu jian du qu excel- bao cun wei csv ge shi - chu li zui hou yi ge zi duan wei null de qing kuang he biao tou - gen ju excel jie gou jian biao - sheng cheng sqlldr de kong zhi wen jian - yong sqlldr ming ling dao ru shu ju
  zhe ge xing neng sui ran mei you sql de bcp kuai , dan huan shi xiang dang ke guan de , zai wo ji qi shang 1 wan duo shu ju bu dao 4 miao , er qie dao ru guo cheng dai ma bi jiao jian dan , ye tong yang mei you xun huan pin jie sql cha ru na me nan yi wei hu 。
  zhe li ye ti ge wen ti : chu li csv wen jian de biao tou he zui hou yi ge zi duan wei null de qing kuang shi fou ke yi you hua ? chu le wo dai ma zhong de li zi , wo shi zai xiang bu chu qi ta ban fa 。
  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;
  // yin yong -com-microsoft excel objects 11.0
  namespace WindowsApplication5
  {
   public partial class Form1 : Form
   {
   public Form1()
   {
   InitializeComponent();
   }
  
   /// <SUMMARY>
   /// excel dao ru dao oracle
   /// </SUMMARY>
   /// <PARAM name="excelFile"> wen jian ming </PARAM>
   /// <PARAM name="sheetName">sheet ming </PARAM>
   /// <PARAM name="sqlplusString">oracle ming ling sqlplus lian jie chuan </PARAM>
   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);
  
   // huo qu excel dui xiang
   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));
  
   // bao cun wei csv lin shi wen jian
   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();
   // du qu csv wen jian , xu yao jiang biao tou qu diao , bing qie jiang zui hou yi lie wei null de zi duan chu li wei xian shi de null, fou ze oracle bu hui shi bie , zhe ge bu zhou you mei you hao de ti huan fang fa ?
   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);
   ms.Write(bytes, 0, bytes.Length);
   ms.Close();
  
   // huo qu excel biao jie gou
   string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
   OleDbConnection conn = new OleDbConnection(strConn);
   conn.Open();
   System.Data.DataTable table = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,
   new object[] { null, null, sheetName+"$", null });
  
   // sheng cheng sqlldr yong dao de kong zhi wen jian , wen jian jie gou shen kao sql*loader gong neng , ben shi li yi dou hao fen ge csv, shu ju dai dou hao de yong yin hao kuo qi lai 。
   string strControl = "load data\r\ninfile &apos;{0}&apos; \r\nappend into table {1}\r\n"+
   "FIELDS TERMINATED BY &apos;,&apos; OPTIONALLY ENCLOSED BY &apos;\"&apos;\r\n(";
   strControl = string.Format(strControl, strCsvPath,sheetName);
   foreach (System.Data.DataRow drowColumns in table.Select("1=1", "Ordinal_Position"))
   {
   strControl += drowColumns["Column_Name"].ToString() + ",";
   }
   strControl = strControl.Substring(0, strControl.Length - 1) + ")";
   bytes=System.Text.Encoding.Default.GetBytes(strControl);
   ms= System.IO.File.Create(strCtlPath);
   ms.Write(bytes, 0, bytes.Length);
   ms.Close();
   // sheng cheng chu shi hua oracle biao jie gou de wen jian
   string strSql = @"drop table {0};
   create table {0}
   (";
   strSql = string.Format(strSql, sheetName);
   foreach (System.Data.DataRow drowColumns in table.Select("1=1", "Ordinal_Position"))
   {
   strSql += drowColumns["Column_Name"].ToString() + " varchar2(255),";
   }
   strSql = strSql.Substring(0, strSql.Length - 1) + ");\r\nexit;";
   bytes = System.Text.Encoding.Default.GetBytes(strSql);
   ms = System.IO.File.Create(strSqlPath);
   ms.Write(bytes, 0, bytes.Length);
   ms.Close();
  
   // yun xing sqlplus, chu shi hua biao
   System.Diagnostics.Process p = new System.Diagnostics.Process();
   p.StartInfo = new System.Diagnostics.ProcessStartInfo();
   p.StartInfo.FileName = "sqlplus";
   p.StartInfo.Arguments = string.Format("{0} @{1}", sqlplusString, strSqlPath);
   p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.CreateNoWindow = true;
   p.Start();
   p.WaitForExit();
   // yun xing sqlldr, dao ru shu ju
   p = new System.Diagnostics.Process();
   p.StartInfo = new System.Diagnostics.ProcessStartInfo();
   p.StartInfo.FileName = "sqlldr";
   p.StartInfo.Arguments = string.Format("{0} {1}", sqlplusString, strCtlPath);
   p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   p.StartInfo.RedirectStandardOutput = true;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.CreateNoWindow = true;
   p.Start();
   System.IO.StreamReader r = p.StandardOutput;// jie qu shu chu liu
   string line = r.ReadLine();// mei ci du qu yi xing
   textBox3.Text += line + "\r\n";
   while (!r.EndOfStream)
   {
   line = r.ReadLine();
   textBox3.Text += line + "\r\n";
   textBox3.Update();
   }
   p.WaitForExit();
   // ke yi zi xing jie jue diao lin shi wen jian csv,ctl he sql, dai ma lue qu
   }
   private void button1_Click(object sender, EventArgs e)
   {
   TransferData(@"D:\test.xls", "Sheet1", "username/password@servicename");
   }
  
   }
  }【原文】【汉音对照
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
ru he gao xiao de jiang excel dao ru dao oracle? he qian liang tian de SqlBulkCopy dao ru dao sqlserver dui ying ,oracle ye you zi shen de fang fa , zhi shi shao wei fu duo xie . na jiu shi shi yong oracle de sql*loader gong neng , er sqlldr zhi zhi chi lei si csv ge shi de shu ju , suo yi yao zi ji ba excel zhuan huan yi xia 。 shi xian bu zhou : yong com zu jian du qu excel- bao cun wei csv ge shi - chu li zui hou yi ge zi duan wei null de qing kuang he biao tou - gen ju excel jie gou jian biao - sheng cheng sqlldr de kong zhi wen jian - yong sqlldr ming ling dao ru shu ju zhe ge xing neng sui ran mei you sql de bcp kuai , dan huan shi xiang dang ke guan de , zai wo ji qi shang 1 wan duo shu ju bu dao 4 miao , er qie dao ru guo cheng dai ma bi jiao jian dan , ye tong yang mei you xun huan pin jie sql cha ru na me nan yi wei hu 。 zhe li ye ti ge wen ti : chu li csv wen jian de biao tou he zui hou yi ge zi duan wei null de qing kuang shi fou ke yi you hua ? chu le wo dai ma zhong de li zi , wo shi zai xiang bu chu qi ta ban fa 。 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; // yin yong -com-microsoft excel objects 11.0 namespace WindowsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <SUMMARY> /// excel dao ru dao oracle /// </SUMMARY> /// <PARAM name="excelFile"> wen jian ming </PARAM> /// <PARAM name="sheetName">sheet ming </PARAM> /// <PARAM name="sqlplusString">oracle ming ling sqlplus lian jie chuan </PARAM> 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 +"[url=file://\\]\\"+strFileName[/url] + ".csv"; string strCtlPath = strTempDir + "\\" + strFileName + ".Ctl"; string strSqlPath = strTempDir + "\\" + strFileName + ".Sql"; if (System.IO.File.Exists(strCsvPath)) System.IO.File.Delete(strCsvPath); // huo qu excel dui xiang 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)); // bao cun wei csv lin shi wen jian 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(); // du qu csv wen jian , xu yao jiang biao tou qu diao , bing qie jiang zui hou yi lie wei null de zi duan chu li wei xian shi de null, fou ze oracle bu hui shi bie , zhe ge bu zhou you mei you hao de ti huan fang fa ? 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); ms.Write(bytes, 0, bytes.Length); ms.Close(); // huo qu excel biao jie gou string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); System.Data.DataTable table = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, new object[] { null, null, sheetName+"$", null }); // sheng cheng sqlldr yong dao de kong zhi wen jian , wen jian jie gou shen kao sql*loader gong neng , ben shi li yi dou hao fen ge csv, shu ju dai dou hao de yong yin hao kuo qi lai 。 string strControl = "load data\r\ninfile &apos;{0}&apos; \r\nappend into table {1}\r\n"+       "FIELDS TERMINATED BY &apos;,&apos; OPTIONALLY ENCLOSED BY &apos;\"&apos;\r\n(";   strControl = string.Format(strControl, strCsvPath,sheetName); foreach (System.Data.DataRow drowColumns in table.Select("1=1", "Ordinal_Position")) { strControl += drowColumns["Column_Name"].ToString() + ","; } strControl = strControl.Substring(0, strControl.Length - 1) + ")"; bytes=System.Text.Encoding.Default.GetBytes(strControl); ms= System.IO.File.Create(strCtlPath); ms.Write(bytes, 0, bytes.Length); ms.Close(); // sheng cheng chu shi hua oracle biao jie gou de wen jian string strSql = @"drop table {0};              create table {0}     ("; strSql = string.Format(strSql, sheetName); foreach (System.Data.DataRow drowColumns in table.Select("1=1", "Ordinal_Position")) { strSql += drowColumns["Column_Name"].ToString() + " varchar2(255),"; } strSql = strSql.Substring(0, strSql.Length - 1) + ");\r\nexit;"; bytes = System.Text.Encoding.Default.GetBytes(strSql); ms = System.IO.File.Create(strSqlPath); ms.Write(bytes, 0, bytes.Length); ms.Close(); // yun xing sqlplus, chu shi hua biao System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = new System.Diagnostics.ProcessStartInfo(); p.StartInfo.FileName = "sqlplus"; p.StartInfo.Arguments = string.Format("{0} @{1}", sqlplusString, strSqlPath); p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.Start(); p.WaitForExit(); // yun xing sqlldr, dao ru shu ju p = new System.Diagnostics.Process(); p.StartInfo = new System.Diagnostics.ProcessStartInfo(); p.StartInfo.FileName = "sqlldr"; p.StartInfo.Arguments = string.Format("{0} {1}", sqlplusString, strCtlPath); p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.Start(); System.IO.StreamReader r = p.StandardOutput;// jie qu shu chu liu string line = r.ReadLine();// mei ci du qu yi xing textBox3.Text += line + "\r\n"; while (!r.EndOfStream) { line = r.ReadLine(); textBox3.Text += line + "\r\n"; textBox3.Update(); } p.WaitForExit(); // ke yi zi xing jie jue diao lin shi wen jian csv,ctl he sql, dai ma lue qu } private void button1_Click(object sender, EventArgs e) { TransferData(@"D:\test.xls", "Sheet1", "[url=mailto:username/password@servicename]username/password@servicename[/url]"); } } }
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚性感的燕子(3)
时尚性感的燕子(2)
纯美的女生生活写真(14)
纯美的女生生活写真(13)
来自束河[]
恰巧-巧家~~
高尔夫
行摄匆匆天子山
 
>>返回首页<<
 为你推荐
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有