c# winform 把Excel里的数据导入到Oracle数据库里,读取excel到dataset
winform程序,点击程序上的按钮,找到Excel文件,打开后把这个Excel里的数据导入到oracle数据库里
还有一个是读到DataTable里
用oledb方式读取excel到datatable,然后再写入数据库。
C#代码
- #region 导入Excel数据到数据库
- private void button3_Click(object sender, EventArgs e)
- {
- DataSet ds;
- OpenFileDialog openFileDialog1= new OpenFileDialog();
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
openFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1; - 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'"; // HDR=YES 为表的第一行不作为数据读入
- 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,然后再写入数据库。
C#代码
- 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;
- }