> 데이터 베이스 > MySQL 튜토리얼 > C# Excel 导入到 Access数据库表(winForm版)

C# Excel 导入到 Access数据库表(winForm版)

WBOY
풀어 주다: 2016-06-07 15:37:56
원래의
1770명이 탐색했습니다.

直接贴代码吧。 /// summary /// 获取Excel文件 /// /summary /// param name=sender/param /// param name=e/param private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = Excel文件(*.xls)|

直接贴代码吧。

 

        ///


        /// 获取Excel文件
        ///

        ///
        ///
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel文件(*.xls)|*.xls";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filePath = dlg.FileName;
                this.textBox1.Text = filePath;
            }
        }

        ///
        /// 导入Excel文件
        ///

        ///
        ///
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("请选择导入数据的Execl文件");
            }
            else
            {
                try
                {
                    OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
                    connectStringBuilder.DataSource = this.textBox1.Text.Trim();
                    connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
                    connectStringBuilder.Add("Extended Properties", "Excel 8.0");
                    using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
                    {
                        DataSet ds = new DataSet();
                        string sql = "Select * from [Sheet1$]";
                        OleDbCommand cmdLiming = new OleDbCommand(sql, cn);
                        cn.Open();
                        using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
                        {
                            ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] { "Sheet1" });
                            DataTable dt = ds.Tables["Sheet1"];
                            if (dt.Rows.Count > 0)
                            {
                                for (int i = 0; i                                 {
                                    //写入数据库数据
                                    string MySql = "insert into ClientInfo values('"+dt.Rows[i]["姓名"].ToString()+"','"+dt.Rows[i]["姓名"].ToString()
                                        +"','0','"+dt.Rows[i]["备注"].ToString()+"','0','"+i.ToString()+"')";
                                    new DataAccess().SQLExecute(MySql);
                                }
                                MessageBox.Show("数据导入成功!");
                            }
                            else
                            {
                                MessageBox.Show("请检查你的Excel中是否存在数据");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

 

 ///


        /// 数据操作通用类
        ///

        ///
        ///
        public bool SQLExecute(string sql)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection(CONNECT_STRING);
                conn.Open();
                OleDbCommand comm = new  OleDbCommand ();
                comm.Connection = conn;
                comm.CommandText = sql;
                comm.ExecuteNonQuery();
                comm.Connection.Close();
                conn.Close();
                return true;
            }
            catch
            {
                return false;

            }
        }

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿