//打开Excel,把数据放进dataGridView中
        private void bind_stu(string fileName)
        {
            DelegateDataGridViewWRLUI delegateDataGridViewWRLUI = delegate
            {
                //取消跨线程检查,不使用委托的方法,解决“线程间操作无效,从不是创建控件的线程访问它”的问题
                //进行非安全线程访问时,运行环境就不去检验它是否是线程安全的
                //Control.CheckForIllegalCrossThreadCalls = false;//方法一,不建议,转 利用委托机制实现线程安全。

                //office2007版本以上
                string           strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'";
                OleDbDataAdapter da      = new OleDbDataAdapter("SELECT *  FROM [sheet1$]", strConn);
                DataSet          ds      = new DataSet();
                try
                {
                    da.Fill(ds);
                    dt = ds.Tables[0];
                }
                catch (Exception err)
                {
                    MessageBox.Show("操作失败!" + err.ToString());
                }
                //textBox1.Text = "" + fileName;
            };

            this.dataGridView1.Invoke(delegateDataGridViewWRLUI);
        }
Example #2
0
        //打开Excel,把数据放进dataGridView中
        private void bind_stu(string fileName)
        {
            DelegateDataGridViewWRLUI delegateDataGridViewWRLUI = delegate
            {
                //取消跨线程检查,不使用委托的方法,解决“线程间操作无效,从不是创建控件的线程访问它”的问题
                //进行非安全线程访问时,运行环境就不去检验它是否是线程安全的
                //Control.CheckForIllegalCrossThreadCalls = false;//方法一,不建议,转 利用委托机制实现线程安全。

                //office2007版本以上
                string           strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'";
                OleDbDataAdapter da      = new OleDbDataAdapter("SELECT *  FROM [sheet1$]", strConn);
                DataSet          ds      = new DataSet();
                this.dataGridView1.DataSource = null;
                this.dataGridView1.Rows.Clear();
                //DataTable a = SQLDBhelper.ExecuteDataTable("select * from stupasswordtable where 1=2");
                //dataGridView1.DataSource = a;
                //DataTable dt = (DataTable)dataGridView1.DataSource;
                //dt.Rows.Clear();
                //dataGridView1.DataSource = dt;
                try
                {
                    da.Fill(ds);
                    dt = ds.Tables[0];
                    if (dt.Rows.Count != 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            dataGridView1.Rows.Add(dt);
                            dataGridView1.Rows[i].Cells[0].Value  = dt.Rows[i]["账号"].ToString();
                            dataGridView1.Rows[i].Cells[1].Value  = dt.Rows[i]["密码"].ToString();
                            dataGridView1.Rows[i].Cells[2].Value  = dt.Rows[i]["姓名"].ToString();
                            dataGridView1.Rows[i].Cells[3].Value  = dt.Rows[i]["性别"].ToString();
                            dataGridView1.Rows[i].Cells[4].Value  = dt.Rows[i]["学号"].ToString();
                            dataGridView1.Rows[i].Cells[5].Value  = dt.Rows[i]["年级"].ToString();
                            dataGridView1.Rows[i].Cells[6].Value  = dt.Rows[i]["专业"].ToString();
                            dataGridView1.Rows[i].Cells[7].Value  = dt.Rows[i]["班别"].ToString();
                            dataGridView1.Rows[i].Cells[8].Value  = dt.Rows[i]["电话号码"].ToString();
                            dataGridView1.Rows[i].Cells[9].Value  = dt.Rows[i]["邮箱地址"].ToString();
                            dataGridView1.Rows[i].Cells[10].Value = dt.Rows[i]["自我介绍"].ToString();
                            dataGridView1.Rows[i].Cells[11].Value = dt.Rows[i]["学院"].ToString();
                            dataGridView1.Rows[i].Cells[12].Value = dt.Rows[i]["团队编号"].ToString();
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show("操作失败!" + err.ToString());
                }
                textBox1.Text = "" + fileName;
            };

            this.dataGridView1.Invoke(delegateDataGridViewWRLUI);
        }