一个Excel中有多个sheet,每一个sheet存放的是一个表中的数据。
Excel中存放的表有可能是一个或多个表空间的表。
sheet的命名规则是:表名|表空间名。

解决方案 »

  1.   

     private string OpenFileDialogByExcel()
            {
                //打开一个文件选择框
                ofd = new OpenFileDialog();
                //打开文本框的标题
                ofd.Title = "Excel文件";
                ofd.FileName = "";
                //指定打开文本框的路径为我的电脑
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
                //打开文件的格式
                ofd.Filter = "Excel文件(*.xls)|*.xls";            ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
                ofd.CheckFileExists = true;  //验证路径有效性
                ofd.CheckPathExists = true; //验证文件有效性
                strName = string.Empty;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    strName = ofd.FileName; //获取文件路径
                }            if (strName == "")
                {
                    MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                    return null;
                }            return strName;        }
            public void EcxelToAccess(string filePath)
            {
                bool IsSuccess = false; //判断添加是否成功
                //根据路径打开一个Excel文件并将数据填充到DataSet中
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
                string strExcel = "";
                strExcel = "select  * from   [sheet1$]";
                OleDbDataReader readerExcel = ExecuterReader(strConn, CommandType.Text, strExcel, null);
               // dataGridView1.Rows.Clear();
                if (readerExcel.HasRows == true)
                {
                    List<string> list = new List<string>();
                    if (readerExcel.Read())
                    {
                        while (readerExcel.Read())
                        {
                            list.Clear();   //循环读取之前就清空list集合的值                      
                            UserInfo _userInfo = new UserInfo();
                            _userInfo.Id = int.Parse(readerExcel[0].ToString());
                            _userInfo.Expertname = readerExcel[1].ToString();
                            _userInfo.Contact = readerExcel[2].ToString();
                            _userInfo._职称 = readerExcel[3].ToString();
                            _userInfo._工作单位 = readerExcel[4].ToString();
                            _userInfo._专业 = readerExcel[5].ToString();
                            IsSuccess = IputToAccess(_userInfo);    //添加到数据库中去                        string id = _userInfo.Id.ToString();    //获取用户编号
                            string name = _userInfo.Expertname;       //获取用户名
                            string 联系方式 = _userInfo.Contact;         //获取用户密码
                            string __职称 = _userInfo._职称;
                            string __工作单位 = _userInfo._工作单位;
                            string __专业 = _userInfo._专业;
                            list.AddRange(new string[]
                                {
                                    id,name,联系方式,__职称,__工作单位,__专业
                                });
                            int index = dataGridView2.Rows.Add();
                            //为datagirdview创建行
                            DataGridViewRow newRow = dataGridView2.Rows[index];
                            //为datagirdview行的每个单元格内容赋值
                            for (int j = 0; j < list.Count; j++)
                            {
                                newRow.Cells[j].Value = list[j].ToString(); //为每个单元格进行赋值
                            }
                        }
                        if (IsSuccess == true)
                        {
                            MessageBox.Show("导入成功");
                        }
                        else
                        {
                            MessageBox.Show("导入失败!");
                        }
                    }            }        }
            public OleDbDataReader ExecuterReader(string connectionstring, CommandType type, string cmdText, params OleDbDataReader[] commandParameters)
            {
                OleDbCommand cmd = new OleDbCommand();  //准备命令
                OleDbConnection conn = new OleDbConnection(connectionstring);  //创建连接            // 在这里使用try/catch处理是因为如果方法出现异常,则SqlDataReader就不存在,
                //CommandBehavior.CloseConnection的语句就不会执行,触发的异常由catch捕获。
                //关闭数据库连接,并通过throw再次引发捕捉到的异常。            //判断数据库连接状态
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();            //打开数据库连接
                }
                cmd.Connection = conn;      //建立连接
                cmd.CommandText = cmdText;  //指定命令文本
                cmd.CommandType = type;  //指定命令类型            if (commandParameters != null)
                {
                    foreach (OleDbDataReader param in commandParameters)  //OleDbParameter
                    {
                        cmd.Parameters.Add(param);  //添加参数到参数列表中
                    }
                }            OleDbDataReader reader = cmd.ExecuteReader(); //执行命令
                //返回一个数据库记录集
                return reader;
            }       
                 /// <summary>
            /// 添加数据到access
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void tsmInputData_Click(object sender, EventArgs e)
            {
                //打开对话框选择excel文件
                OpenFileDialogByExcel();
                if (strName != "")    //判断路径是否为空
                {
                    EcxelToAccess(strName); //导入数据到数据库中去
                }
            }        private void frmInputDataToAccess_Load(object sender, EventArgs e)
            {        }
            private void button4_Click(object sender, EventArgs e)
            {
                ExportDataGridview(dataGridView1, true);
            }        private void button5_Click(object sender, EventArgs e)
            {
                //打开对话框选择excel文件
                OpenFileDialogByExcel();
                if (strName != "")    //判断路径是否为空
                {
                    EcxelToAccess(strName); //导入数据到数据库中去
                }
            }
            private void visible()
            {
                textBox1.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                comboBox1.Visible = false;
                comboBox2.Visible = false;
                comboBox3.Visible = false;
                dataGridView1.Visible = false;
                button1.Visible = false;
                button2.Visible = false;
                button3.Visible = false;
                button4.Visible = false;
                button5.Visible = false;
                button6.Visible = false;        }
            private bool IputToAccess(UserInfo _userInfo)
            {
                int rows = 0;
                //把数据库连接的数据源指向excel文件名
                string str_connectionString = "server=.;database=test;uid=sa;pwd=123";
                //添加语句
                string str_sql = "insert into [expert](expertname,contact,职称,工作单位,专业) values('" + _userInfo.Expertname + "','" + _userInfo.Contact +
                    "','" + _userInfo._职称 + "','" + _userInfo._工作单位 + "','" + _userInfo._专业 + "')";
                //异常捕获机制
                try
                {
                    //创建连接
                    SqlConnection conn = new SqlConnection(str_connectionString);
                    conn.Open();                //创建命令对象
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;//指定连接
                    cmd.CommandText = str_sql;//指定SQL语句
                    cmd.CommandType = CommandType.Text;//执行命令类型
                    rows = cmd.ExecuteNonQuery();   //执行命令
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("错误是:" + ex.Message);
                }            return rows > 0 ? true : false;        }
      

  2.   

    意思是从EXCEL导入到 数据库。我有这方面的例子。百度一下。。弄了半天才做出来的。要的q我 310500514
      

  3.   

    表空间只是一个逻辑概念,重要的是把数据取出来再插入到相应的表当中。
    1、读取Excel
    2、插入oracle
    代码很简单的,半个小时搞定吧。自己试试看,不行的话,可以百度一下,锻炼哈子。
      

  4.   

    /// <summary>  
            /// 解析Excel,根据OleDbConnection直接连Excel  
            /// </summary>  
            /// <param name="filePath"></param>  
            /// <param name="name"></param>  
            /// <returns></returns>  
            public static DataSet LoadDataFromExcel(string filePath, string name)  
            {  
                try  
                {  
                    string strConn;  
                    //   strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=Excel 8.0";  
                    strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\"";  
                   OleDbConnection  OleConn = new OleDbConnection(strConn);  
                    OleConn.Open();  
                    string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等    
                    OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);  
                    DataSet OleDsExcle = new DataSet();  
                    OleDaExcel.Fill(OleDsExcle, name);  
                    OleConn.Close();  
                    return OleDsExcle;  
                }  
                catch (Exception err)  
                {  
                    MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    return null;  
                }  
            }  返回dataSet了,,插入数据库就不用说了吧,,循环插入数据库