将最新版本的 Excel 数据(如 2013)上传到 SQL 服务器 table 而不安装 Access 数据库引擎

Uploading latest version of Excel data like 2013 into SQL Server table without installing Access database engine

我收到这个错误

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

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

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

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

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.