引用命名空间:
using System.Data.OleDb;
界面:
放一个打开按钮,一个显示按钮,一个combobox
后台:
/// <summary>   
      /// 选择相应的Excel文件   (打开Excel按钮事件)
      /// </summary>   
      /// <param name="sender"></param>   
      /// <param name="e"></param>   
        private void button1_Click(object sender, EventArgs e)   
      {   
         try  
         {   
            //获取Excel文件路径和名称   
            OpenFileDialog odXls = new OpenFileDialog();   
            // 指定相应的打开文档的目录   
            odXls.InitialDirectory = "C:\\";   
            // 设置文件格式   
            odXls.Filter = "Excel files (*.xls)|*.xls";   
            odXls.FilterIndex = 2;   
            odXls.RestoreDirectory = true;   
            if (odXls.ShowDialog() == DialogResult.OK)   
            {   
               label2.Text = odXls.FileName;   
               OleDbConnection oledbConn = null;   
               string sConnString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + odXls.FileName + ";Extended Properties=Excel 5.0;Persist Security Info=False";   
               oledbConn = new OleDbConnection(sConnString);   
               oledbConn.Open();   
               DataTable dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });   
               comboBox1.Items.Clear();   
               foreach (DataRow dr in dt.Rows)   
               {   
                  comboBox1.Items.Add((String)dr["TABLE_NAME"]);   
               }   
               if (comboBox1.Items.Count > 0)   
                  comboBox1.SelectedIndex = 0;   
            }   
         }   
         catch (Exception Ex)   
         {
             MessageBox.Show(Ex.Message);
         }   
      }
        /// <summary>   
        ///  读取相应的表名的Excel文件中数据到当前DataGridview中显示   (显示数据按钮事件)
        /// </summary>   
        /// <param name="sender"></param>   
        /// <param name="e"></param>   
        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection ole = null;
            OleDbDataAdapter da = null;
            DataTable dt = null;
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
                             + "Data Source=" + label2.Text.Trim() + ";"
                             + "Extended Properties=Excel 5.0";
            string sTableName = comboBox1.Text.Trim();
            string strExcel = "select * from [" + sTableName + "]";
            try
            {
                ole = new OleDbConnection(strConn);
                ole.Open();
                da = new OleDbDataAdapter(strExcel, ole);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                ole.Close();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            finally
            {
                if (ole != null)
                    ole.Close();
            }
        }
- 本文标题: winform打开Excel读取数据并显示到datagridview中
- 文章分类:【WinForm/WPF】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.