ASP.NET导入Excel数据到sqlserver数据库,报未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序

     今天完成了在ASP.NET导入Excel文件数据的功能,详细实现是,

  <td >      
                    <asp:Button ID="btn_Imp" runat="server" Text="导入" Style="display: none" OnClick="btnImp_Click"
                        CssClass="btn_yellow" />
                    <button id="AttachLink" onmouseover="attOnmouseover(this);" style="cursor: hand;
                        text-decoration: underline;" onclick="fClick();" onmouseout="this.style.textDecoration='none'"
                        name="AttachLink" class="btn_yellow">
                        导入Excel</button>
                    <asp:FileUpload ID="f" runat="server" hideFocus onmouseover="AttachLink.style.textDecoration='underline';"
                        Style="position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);
                        opacity: 0; 70px; right: 66px;" onmouseout="AttachLink.style.textDecoration='none';"
                        onchange="fileChanged()" Width="1px" />
                </td>

 <script type="text/javascript">

        function fileChanged() {
            var btn_Imp = document.getElementById("btn_Imp");
            btn_Imp.click();
        }
        function fClick() {
            var f = document.getElementById("f");
            f.click();
        }
        function attOnmouseover(obj)
        { var f = document.getElementById("f"); obj.style.textDecoration = 'underline'; f.style.pixelLeft = event.x - 60; }


    </script>

在界面定义一个"导入Excel"按钮为普通button按钮,上传文件的控件为隐藏状态,点击”导入Excel“按钮将调用js脚本中定义的fClick()函数,在此函数中将触发上传控件的点击事件,弹出选择文件对话框,选择文件,当选择文件完毕后,将调用导入按钮的服务器控件的点击事件,调用后台的点击事件操作方法。

   只贴加载Excel的代码 :

  public static DataSet LoadDataFromExcel(string filePath)
    {
        try
        {
            string strConn;
            strConn = "Provider= Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'"; //支持2007及以上版本
            // strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";   //支持2003
            OleDbConnection OleConn = new OleDbConnection(strConn);
            OleConn.Open();
            DataTable dt = OleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            string tableName = dt.Rows[0][2].ToString().Trim();
            String sql = "SELECT * FROM  [" + tableName + "]";//可是更改Sheet名称,比如sheet2,等等                 
            OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
            DataSet OleDsExcle = new DataSet();
            OleDaExcel.Fill(OleDsExcle, "Sheet1");
            OleConn.Close();
            return OleDsExcle;
        }
        catch (Exception err)
        {
            throw new Exception(err.Message);
        }
    }

导入成功以后,基本这个小项目的所有功能都开发完成了,请IT部门帮我设定了一个固定IP,我以本机作为服务器,在本机IIS上发布了一个测试版,结果上传Excel数据报错,

错误信息“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”,在网上找到了答案:在IIS中设置应用程序:应用程序池 ------>选择高级设置 --------->启用32位应用程序  ------->true   问题得到了解决,越来越有踏实感,觉得自己能在技术路上走得更远点。

原文地址:https://www.cnblogs.com/huizi/p/3193522.html