winform程序,点击程序上的按钮,找到Excel文件,打开后把这个Excel里的数据导入到oracle数据库里
#region 导入Excel数据到数据库
private void button3_Click(object sender, EventArgs e)
{
DataSet ds;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ds = ImportExcel(this.openFileDialog1.FileName);//获得Excel
}
else
{
return;
}
int odr = 0;
OracleConnection conn = dbc.getConnection();//获得conn连接
try
{
conn.Open();
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO kk.kkhmd (xh,hpzl,hphm,bz,larq,fdjh,clpp,cjh,jdcsyr,cllx,csys) VALUES(:xh,:hpzl,:hphm,:bz,:larq,:fdjh,:clpp,:cjh,:jdcsyr,:cllx,:csys) ";//删除记录
int dsLength = ds.Tables[0].Rows.Count;//获得Excel中数据长度
for (int i = 1; i < dsLength; i++)
{
cmd.Parameters.Add("xh", OracleType.VarChar).Value = ds.Tables[0].Rows[i][0];
cmd.Parameters.Add("hpzl", OracleType.VarChar).Value = ds.Tables[0].Rows[i][1];
cmd.Parameters.Add("hphm", OracleType.VarChar).Value = ds.Tables[0].Rows[i][2];
cmd.Parameters.Add("bz", OracleType.VarChar).Value = ds.Tables[0].Rows[i][3];
cmd.Parameters.Add("larq", OracleType.DateTime).Value = ds.Tables[0].Rows[i][4];
cmd.Parameters.Add("fdjh", OracleType.VarChar).Value = ds.Tables[0].Rows[i][5];
cmd.Parameters.Add("clpp", OracleType.VarChar).Value = ds.Tables[0].Rows[i][6];
cmd.Parameters.Add("cjh", OracleType.VarChar).Value = ds.Tables[0].Rows[i][7];
cmd.Parameters.Add("jdcsyr", OracleType.VarChar).Value = ds.Tables[0].Rows[i][8];
cmd.Parameters.Add("cllx", OracleType.VarChar).Value = ds.Tables[0].Rows[i][9];
cmd.Parameters.Add("csys", OracleType.VarChar).Value = ds.Tables[0].Rows[i][10];
odr = cmd.ExecuteNonQuery();//提交
}
//如果查到了数据,才使控制分页按钮生效
if (odr > 0 )
{
MessageBox.Show("导入成功");
}
conn.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static DataSet ImportExcel(string file)
{
FileInfo fileInfo = new FileInfo(file);
if (!fileInfo.Exists)
return null;
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
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;
}
}
#endregion
还有一个是读到DataTable里
用oledb方式读取excel到datatable,然后再写入数据库。
private DataTable GetData(string strPath)
{
DataTable dtbl = new DataTable();
try
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;";
string strSheetName = "";
using (OleDbConnection con = new OleDbConnection(strCon))
{
con.Open();
DataTable dtbl1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//dataGridView2.DataSource = dtbl1;
strSheetName = dtbl1.Rows[0][2].ToString().Trim();
}
String strCmd = "select * from [" + strSheetName + "]";
OleDbDataAdapter cmd = new OleDbDataAdapter(strCmd, strCon);
cmd.Fill(dtbl);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
return dtbl;
}
黑色头发:http://heisetoufa.iteye.com