通过OleDB读取Excel到Dataset里面

/// <summary>
        /// 读取Excel内容到数据集里面
        /// </summary>
        /// <param name="filepath">Excel文件路径</param>
        /// <returns>Excel内容</returns>
        public static DataSet ReadExcel(string filepath)
        {
            string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
            if (filepath.Contains(".xlsx"))
            {
                conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
            }
            string strMASTCon = "SELECT * FROM [Sheet1$]";
            DataSet dsSrc;

            OleDbConnection myConn = new OleDbConnection(conn);
            try
            {
                myConn.Open();
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strMASTCon, myConn);
                dsSrc = new DataSet();
                myCommand.Fill(dsSrc, "[Sheet1$]");
                myConn.Close();

                return dsSrc;
            }
            catch
            {
                return null;
            }
        }

 

//=================================================================

 

解决 Application is busy (RPC_E_CALL_REJECTED 0x80010001)  Call was rejected by callee (RPC_E_SERVERCALL_RETRYLATER 0x8001010A)
        /* Fixing 'Application is Busy' and 'Call was Rejected By Callee' Errors
         * If you programatically call into Visual Studio automation from an external (out-of-proc), multi-threaded application, you can occasionally get the errors "Application is busy" or "Callee was rejected by caller." These errors occur due to threading contention issues between external multi-threaded applications and Visual Studio. They can be eliminated by implementing IOleMessageFilter error handlers in your Visual Studio automation application. (Not to be confused with System.Windows.Forms.IMessageFilter.)
         *
         * To fix errors
         *
         * 1.    Add the following class to your application.
         * 2.    Add a COM reference to "Microsoft Development Environment 8.0." This adds references to EnvDTE and EnvDTE80 to your solution.
         * 3.    In the code, create an instance of EnvDTE80, as outlined in the following example.
         * 4.    Call Message.Register to handle thread errors.
         * 5.    Call your automation code as usual.
         * 6.    When your automation code is finished, call Message.Revoke to remove the thread error handlers.
         */

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值