【问题标题】:Uploading latest version of Excel data like 2013 into SQL Server table without installing Access database engine在不安装 Access 数据库引擎的情况下将最新版本的 Excel 数据(如 2013)上传到 SQL Server 表中
【发布时间】:2017-07-06 06:13:18
【问题描述】:

我收到这个错误

“Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册

当我尝试使用 .NET 将 Excel 2013 数据上传到 SQL Server 表中时出现此错误。我需要在不安装 Access 数据库引擎的情况下解决此错误。

【问题讨论】:

    标签: sql asp.net sql-server excel-2013


    【解决方案1】:

    这是一种选择 - https://www.simple-talk.com/sql/ssis/moving-data-from-excel-to-sql-server-10-steps-to-follow/

    另一种选择是使用 OleDbConnection 从 excel 文件中读取数据,然后将该数据插入到 sql server -Accessing Excel 2013 File in ASP.NET

    【讨论】:

      【解决方案2】:
      Thank you for your response Mr.ArunGeorge. I solved my question .
      
      I would use EPPlus and SqlBulkCopy for this. It saves having to install anything on the server, like Office or the Access database connector (ACE), and SqlBulkCopy is a very fast way to insert data into SQL Server from C# (or VB) code. Also, EPPlus will create an Excel workbook in memory from a stream which means that you don't have to save the uploaded Excel file to the server, thereby saving you from having to mess about with file permissions or clearing out saved files that are no longer needed.
      
      You can get EPPlus from Nuget (install-package EPPlus). Once you have it, here's a very simple Web Forms example showing how to use it. First, the aspx file which only has a FileUpload and a Button:
      
      
      <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
          <asp:FileUpload ID="Upload" runat="server" />
          <asp:Button ID="Button1" runat="server" Text="Upload" />
      </asp:Content>
      
      Then the code-behind:
      
      protected void Page_Load(object sender, EventArgs e)
      {
          if (IsPostBack && Upload.HasFile)
          {
              if(Path.GetExtension(Upload.FileName).Equals(".xlsx"))
              {
                  var excel = new ExcelPackage(Upload.FileContent);
                  var dt = excel.ToDataTable();
                  using (var conn = new SqlConnection("Server=.;Database=YourDatabase;Integrated Security=SSPI"))
                  {
                      var bulkCopy = new SqlBulkCopy(conn);
                      bulkCopy.DestinationTableName = "YourTable";
                      foreach (DataColumn column in dt.Columns)
                      {
                          //assumes that the Excel column names match the import table exactly
                          bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
                      }
                      conn.Open();
                      bulkCopy.WriteToServer(dt);
                  }
              }
          }
      }
      
      You will need the following additional using statements at the top of the file:
      
      using OfficeOpenXml;
      using System.Data;
      using System.Data.SqlClient;
      And you will need the following extension method. Just create a class file called ExcelPackageExtensions and paste the following code into it:
      
      public static class ExcelPackageExtensions
      {
          public static DataTable ToDataTable(this ExcelPackage package)
          {
              ExcelWorksheet workSheet = package.Workbook.Worksheets.First();
              DataTable table = new DataTable();
              foreach (var firstRowCell in workSheet.Cells[1, 1, 1, workSheet.Dimension.End.Column])
              {
                  table.Columns.Add(firstRowCell.Text);
              }
              for (var rowNumber = 2; rowNumber <= workSheet.Dimension.End.Row; rowNumber++)
              {
                  var row = workSheet.Cells[rowNumber, 1, rowNumber, workSheet.Dimension.End.Column];
                  var newRow = table.NewRow();
                  foreach (var cell in row)
                  {
                      newRow[cell.Start.Column - 1] = cell.Text;
                  }
                  table.Rows.Add(newRow);
              }
              return table;
          }
      }
      You obviously need to change the connection string and table name to suit your needs, and if you have a mismatch between column names in the Excel file and the database (or don't have a header row in the Excel sheet) you will have to map columns individually instead of generically like I have here.
      

      【讨论】:

        猜你喜欢
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多