首页 > 数据库 > mysql教程 > C# Excel 导入到 Access数据库表(winForm版)

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

WBOY
发布: 2016-06-07 15:37:56
原创
1784 人浏览过

直接贴代码吧。 /// 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)
            {
                字符串文件路径 = dlg.FileName;
                this.textBox1.Text = 文件路径;
            }
        }

        ///
        /// 导入Excel文件
        /// 摘要>
        ///
        ///
        private void Button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("请选择导入数据的Execl文件");
            }
            其他
            {
                尝试
                {
                    OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
                    connectStringBuilder.DataSource = this.textBox1.Text.Trim();
                    connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
                    connectStringBuilder.Add("扩展属性", "Excel 8.0");
                    使用 (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
                    {
                        DataSet ds = new DataSet();
                        string sql = "从 [Sheet1$] 选择 *";
                        OleDbCommand cmdLiming = new OleDbCommand(sql, cn);
                        cn.Open();
                        使用 (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 = "插入 ClientInfo 值('" dt.Rows[i]["姓名"].ToString() "','" dt.Rows[i]["姓名"].ToString()
                                        "','0','" dt.Rows[i]["备注"].ToString() "','0','" i.ToString() "')";
                                    new DataAccess().SQLExecute(MySql);
                                }
                                MessageBox.Show("数据导入成功!");
                            }
                            其他
                            {
                                MessageBox.Show("请检查您的Excel中是否存在数据");
                            }
                        }
                    }
                }
                catch(异常前)
                {
                    MessageBox.Show(例如.ToString());
                }
            }
        }

 

 ///
        /// 操作数据通用类
        /// 摘要>
        ///
        /// 返回>
        public bool SQLExecute(string sql)
        {
            尝试
            {
                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();
                返回真;
            }
            抓住
            {
                返回错误;

            }
        }

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板