public static DataSet LoadDataFromExcel(string filePath)
        {
            try
            {
                string strConn;
                strConn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+filePath+";Extended Properties=Excel 8.0";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                String sql = "SELECT * FROM  [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等                   OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                OleDaExcel.Fill(OleDsExcle, "Sheet1");
                OleConn.Close();
                return OleDsExcle;
            }
            catch (Exception err)
            {                MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
                tt.Kill();
                return null;
            }
        }设置断点查看 是OleConn.Open();的时候失败了……

解决方案 »

  1.   

    OleConn.Open()出错、你数据库能打开么?
      

  2.   

    仔细调试一下把,可能出现不能Open的时候,有可能之前已经打开没关闭,还有可能别的进程在用
      

  3.   

    string strConn;
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;"; 
                    OleDbConnection OleConn = new OleDbConnection(strConn);找到问题的关键了,因为我用的是office2007所以应该用上面的语句……