excel 数据表上传到oracle数据库,步骤如下:
1、打开本地excel文件
2、用OleDb连接excel文件
3、将来excel的数据读取到dataset中
4、把dataset 中数据insert到oracle中相应的表中
下面截图说明:

建立项目文件,很简单,就是建立普通的winform项目。其中访问oracle要添加引用System.Data.OracleClient;
vs2010 默认是.net framework 4.0 client profile 。在添加引用时是看不到System.Data.OracleClient;需要在
项目文件上右击,选择属性。会弹出如下对话框:

在target framework 下拉框中 选择.net framework 4。这样后续添加引用时,才能在.net页签看到System.Data.OracleClient;
下面是全部代码
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "1(*.xlsx)|*.xlsx";
openFileDialog1.ShowDialog();//打???方?
this.textBox1.Text = openFileDialog1.FileName;//得到?=路?+名?
}
private void button2_Click(object sender, EventArgs e)
{
try
{
DataSet ds = ImportExcel(this.textBox1.Text);//?excel的?象先放到ds 中
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)//如果ds中是有值的? ?行下面的操作
{
if (ExportInfo(ds))
{
MessageBox.Show("?入?料?成功!");
}
else
{
MessageBox.Show("?入?料?失?!");
}
}
}
}
catch
{
MessageBox.Show("?入?料?失? ??查?入?是否填?正?!");
}
}
public static DataSet ImportExcel(string file)
{
FileInfo fileInfo = new FileInfo(file);
if (!fileInfo.Exists) return null; string strConn = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + file + ";Extended Properties='Excel 12.0; HDR=yes; IMEX=2'";
// 此处用的是excel2010,如果为其他excel版本,请选择相应的连接驱动和字符串
OleDbConnection objConn = new OleDbConnection(strConn);
DataSet dsExcel = new DataSet();
try
{
objConn.Open();
string strSql = "select * from [Sheet1$]";
OleDbDataAdapter odbcExcelDataAdapter = new OleDbDataAdapter(strSql, objConn);
odbcExcelDataAdapter.Fill(dsExcel); return dsExcel;
}
catch (Exception ex)
{
throw ex;
}
}
public static bool ExportInfo(DataSet ds)
{
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)//如果ds中是有值的? ?行下面的操作
{
return Do(ds);//?行成功
}
}
return false;//?行失?
}
public static bool Do(DataSet ds)
{
OracleConnection conNorthwind = new OracleConnection("Data Source=tiptop;User Id=iteqdg;Password=iteqdg;Integrated Security=no;");//??字串
OracleCommand commandNorthwind = new OracleCommand();
try
{
conNorthwind.Open();//打??料???
OracleTransaction tranNorthwind = conNorthwind.BeginTransaction();//?始事?
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].Rows[i];
OracleParameter[] parameters = null;//?了得到插入?料?的?? 定???物件 ?空
string sql = GetSqlString(dr, out parameters);//?行sql -->用out??字得到?? ?到parameters物件上
//插入?料?中
PrepareCommand(commandNorthwind, conNorthwind, tranNorthwind, sql, parameters);
commandNorthwind.ExecuteNonQuery();//?行操作
}
commandNorthwind.Transaction.Commit();//提交事?
conNorthwind.Close();//???料????源
return true;
}
catch//如果有?常 不一定要捕捉?常 但要rollback事?
{
if (commandNorthwind.Transaction != null && conNorthwind != null)
{
commandNorthwind.Transaction.Rollback();//rollback事?
conNorthwind.Close();//???料???
}
return false;
}
}
///
/// 每一行??插入?料?中
///
/// 要插入的?一行ds-datarow?象
/// sql?句和用out??字的???列物件
public static string GetSqlString(DataRow dr, out OracleParameter[] parameters)
{
StringBuilder sb = new S