毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

WinForm将Excel中的表导入orcale

更新时间:2012-9-14:  来源:毕业论文

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

 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;                 }

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。