GridView 绑定 EXCEL 文件 中的数据

            OpenFileDialog localDialog = new OpenFileDialog();
            localDialog.InitialDirectory = "D:\\";
            localDialog.FilterIndex = 1;

            dsEmployee = new DataSet();
            if (localDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = localDialog.FileName;

                string Xls_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + ";Extended Properties='Excel 8.0;IMEX=1'";

                OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);
                try
                {
                    Conn.Open();
                    string sql_str = "select * from [Sheet1$]";
                    OleDbDataAdapter da = new OleDbDataAdapter(sql_str, Conn);
                    da.Fill(dsEmployee, "excel_data");
                    Conn.Close();
                }
                catch
                {
                    if (Conn.State == ConnectionState.Open)
                    {
                        Conn.Close();
                    }
                }
                finally
                {
                    Conn.Dispose();
                }

                this.dataEmployee.DataSource = dsEmployee.Tables[0];

            }

posted @ 2011-06-01 17:50  天添  阅读(180)  评论(0)    收藏  举报