winform 打开excel文件并显示在dataGridView上

以前有过一篇类似的随笔,这次用法与上次不同

try
            {
                OpenFileDialog opXls = new OpenFileDialog();
                opXls.Multiselect = false;//该值确定是否可以选择多个文件
                // 指定打开文档的初始位置  
                opXls.InitialDirectory = "C:\\Users\\lanxiang\\Desktop\\";
                opXls.Title = "请选择文件夹";
                opXls.Filter = "excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
                opXls.FilterIndex = 2;
                opXls.RestoreDirectory = true;
                if (opXls.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = opXls.FileName;
                    OleDbConnection oledbConn = null;
                    string sConnString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + opXls.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" });
                    combox1.Items.Clear();
                    foreach (DataRow dr in dt.Rows)
                    {
                        combox1.Items.Add((String)dr["TABLE_NAME"]);
                    }
                    if (combox1.Items.Count > 0)
                    {
                        combox1.SelectedIndex = 0;
                    }
                    OleDbConnection ole = null;
                    OleDbDataAdapter da = null;
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
                                     + "Data Source=" + textBox1.Text.Trim() + ";"
                                     + "Extended Properties=Excel 5.0";
                    string sTableName = combox1.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);
                        this.dataGridView1.DataSource = dt;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                        }
                        ole.Close();
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.Message);
                    }
                    finally
                    {
                        if (ole != null)
                            ole.Close();
                    }
                    oledbConn.Close();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }

 

posted @ 2021-08-12 19:57  Lee597  阅读(182)  评论(0编辑  收藏  举报