日期:2014-05-18  浏览次数:20517 次

急:导入xls文件到数据库里报错
没有可用的错误消息,结果代码: E_NOINTERFACE(0x80004002)。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.OleDb.OleDbException: 没有可用的错误消息,结果代码: E_NOINTERFACE(0x80004002)。



C# code

string mystring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =" + path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
                    OleDbConnection cnnxls = new OleDbConnection(mystring);
                    string strExcel = "SELECT * FROM [Sheet1$]";
                    OleDbDataAdapter myDa = new OleDbDataAdapter(strExcel, cnnxls);
                    DataSet myDs = new DataSet();
                    myDa.Fill(myDs);  //该行报错



------解决方案--------------------
这是我项目中操作Excel
C# code

        /// <summary>
        /// 获取Excel文件中的数据
        /// </summary>
        /// <param name="filepath">Excel文件的绝对路径</param>
        /// <param name="sheetname">Excel工作薄</param>
        /// <returns></returns>
        public static DataSet ExcelDataSource(string filepath, string sheetname)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection con = new OleDbConnection(strConn);
            OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + sheetname + "$]", con);
            DataSet ds = new DataSet();
            oda.Fill(ds);
            return ds;
        }

------解决方案--------------------
C# code
 DataTable Excel_UserInfo = new DataTable();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
                string strExcel = "select * from [sheet1$]";

                using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
                {
                    DataSet ds = new DataSet();
                    adaptor.Fill(ds);
                    Excel_UserInfo = ds.Tables[0];
                }