论坛首页 编程语言技术论坛

VS C# 2008读取Excel 2007里的数据

浏览 2807 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-06-19  

private OleDbConnection conn = null;

        //建立与Excel的连接
        private void getConnection()
        {       
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.label8.Text.Trim() + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
            if (conn == null)
                conn = new OleDbConnection(connectionString);
        }

        //
        private OleDbDataReader dataReader(string sql)
        {
            this.getConnection();

            OleDbDataReader dataReader = null;
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            try
            {
                conn.Open();
                dataReader = cmd.ExecuteReader();
            }
            catch (Exception ex) { }
            return dataReader;
        }

        //填充发件箱
        private void fillOutBox()
        {
            string sql = "SELECT * FROM [OutBox$]";

            OleDbDataReader dataReader = this.dataReader(sql);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(
            new DataColumn[]{               
                    new DataColumn("mobile",typeof(string)),
                    new DataColumn("message",typeof(string)),
                    new DataColumn("time",typeof(string))
                });

            int i = 0;
            while (dataReader.Read())
            {
                i++;
                if (i == 1)
                    continue;

                DataRow dr = dt.NewRow();
                dr["mobile"] = Convert.ToString(dataReader.GetValue(1));
                dr["message"] = Convert.ToString(dataReader.GetValue(2));
                dr["time"] = Convert.ToString(dataReader.GetValue(3));
               
                dt.Rows.Add(dr);
            }

            this.close();

           this.insertFillOutBox(dt);
        }

        private void insertFillOutBox(DataTable dt)
        {
            this.sHToutBoxBindingSource.DataMember = "SHToutBox";
            this.sHToutBoxBindingSource.DataSource = dt;

            this.sHToutBoxDataGridView.DataSource = this.sHToutBoxBindingSource;

            //把导入的数据插入数据库
            string str_sql = "";
            string mobile = "";
            string message = "";
            string time = "";
            int outBoxRowsCount = this.sHToutBoxDataGridView.Rows.Count;
            for (int j = 0; j < outBoxRowsCount; j++)
            {
                mobile = this.sHToutBoxDataGridView.Rows[j].Cells[0].Value.ToString();
                message = this.sHToutBoxDataGridView.Rows[j].Cells[1].Value.ToString();
                time = this.sHToutBoxDataGridView.Rows[j].Cells[2].Value.ToString();

                str_sql = "INSERT INTO SHToutBox(mobile, message, time)VALUES ('" + mobile + "','" + message + "','" + time + "')";

                this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.CommandText = str_sql;
                this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.Connection.Open();
                this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.ExecuteNonQuery();
                this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.Connection.Close();
            }
        }

 

 

                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "Excel文件(*.xlsx,*.xls)|*.xlsx;*xls";//设置打开文件的格式
                dialog.InitialDirectory = path;//指定初始化路径

                if (File.Exists(path + @"\phone.xlsx"))
                {
                    dialog.FileName = "phone.xlsx";
                }

                DialogResult result = dialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.label8.Text = dialog.FileName;


                    if (this.label8.Text.Trim() != "")
                    {
                        try
                        {
                            this.fillOutBox();

                           this.statusBarPanel1.Text = "导入数据成功";
                            this.statusBarPanel2.Text = "";
                        }
                        catch (Exception exc) { }
                    }
                    else
                    {
                        this.statusBarPanel1.Text = "请选择导出的Excel文件";
                    }
                }

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics