Import dữ liệu từ excel vào sql

In this article

Applies to:

*
SQL Server (all supported versions)
*
Azure Squốc lộ Database

There are several ways lớn import data from Excel files khổng lồ SQL Server or to lớn Azure SQL Database. Some methods let you import data in a single step directly from Excel files; other methods require you to export your Excel data as text (CSV file) before you can import it. This article summarizes the frequently used methods & provides link for more detailed information.

Bạn đang xem: Import dữ liệu từ excel vào sql

List of methods

You can use the following tools to import data from Excel:

Export khổng lồ text first (Squốc lộ Server and Squốc lộ Database)Directly from Excel (SQL Server on-premises only)
Import Flat File WizardSquốc lộ Server Import & Export Wizard
BULK INSERT statementSquốc lộ Server Integration Services (SSIS)
BCPOPENROWSET function
Copy Wizard (Azure Data Factory)
Azure Data Factory
  

If you want khổng lồ import multiple worksheets from an Excel workbook, you typically have lớn run any of these tools once for each sheet.

A complete mô tả tìm kiếm of complex tools & services lượt thích SSIS or Azure Data Factory is beyond the scope of this các mục. To learn more about the solution that interests you, follow the provided liên kết.


Important

For detailed info about connecting to lớn Excel files, và about limitations and known issues for loading data from or to Excel files, see Load data from or lớn Excel with Squốc lộ Server Integration Services (SSIS).


If you don"t have SQL Server installed, or you have Squốc lộ Server but don"t have Squốc lộ Server Management Studio installed, see Download Squốc lộ Server Management Studio (SSMS).

SQL Server Import & Export Wizard

Import data directly from Excel files by stepping through the pages of the SQL Server Import và Export Wizard. Optionally, save sầu the settings as a SQL Server Integration Services (SSIS) package that you can customize & reuse later.

In Squốc lộ Server Management Studio, connect to lớn an instance of the SQL Server Database Engine.

Exp& Databases.

Right-cliông chồng a database.

Point lớn Tasks.

Click one of the following options.

Import Data

Export Data

*

*

For an example of using the wizard lớn import from Excel lớn SQL Server, see Get started with this simple example of the Import & Export Wizard.

To learn about other ways to launch the Import và Export wizard, see Start the SQL Server Import & Export Wizard.

SQL Server Integration Services (SSIS)

If you"re familiar with SSIS and don"t want to lớn run the SQL Server Import and Export Wizard, create an SSIS package that uses the Excel Source và the SQL Server Destination in the data flow.

For more info about these SSIS components, see the following topics:

To start learning how to lớn build SSIS packages, see the tutorial How to lớn Create an ETL Package.

*

OPENROWSET và linked servers


Important

In Azure Squốc lộ Database, you cannot import directly from Excel. You must first export the data to lớn a text (CSV) tệp tin. For examples, see Example.


Note

The ACE provider (formerly the Jet provider) that connects to lớn Excel data sources is intended for interactive client-side use. If you use the ACE provider on SQL Server, especially in automated processes or processes running in parallel, you may see unexpected results.


Distributed queries

Import data directly into lớn SQL Server from Excel files by using the Transact-Squốc lộ OPENROWSET or OPENDATASOURCE function. This usage is called a distributed query.


Important

In Azure SQL Database, you cannot import directly from Excel. You must first export the data to lớn a kiểm tra (CSV) file. For examples, see Example.


Before you can run a distributed query, you have sầu to lớn enable the ad hoc distributed queries VPS configuration option, as shown in the following example. For more info, see ad hoc distributed queries Server Configuration Option.

sp_configure "show advanced options", 1;RECONFIGURE;GOsp_configure "Ad Hoc Distributed Queries", 1;RECONFIGURE;GOThe following code sample uses OPENROWSET lớn import the data from the Excel Sheet1 worksheet into a new database table.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENROWSET("vuialo.net.ACE.OLEDB.12.0", "Excel 12.0; Database=C:TempData.xlsx", );GOHere"s the same example with OPENDATASOURCE.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENDATASOURCE("vuialo.net.ACE.OLEDB.12.0", "Data Source=C:TempData.xlsx;Extended Properties=Excel 12.0")...;GOTo append the imported data to lớn an existing table instead of creating a new table, use the INSERT INTO ... SELECT ... FROM ... syntax instead of the SELECT ... INTO ... FROM ... syntax used in the preceding examples.

To query the Excel data without importing it, just use the standard SELECT ... FROM ... syntax.

For more info about distributed queries, see the following topics:

Linked servers

You can also configure a persistent connection from Squốc lộ Server to the Excel tệp tin as a linked server. The following example imports the data from the Data worksheet on the existing Excel linked hệ thống EXCELLINK inlớn a new SQL Server database table named Data_ls.

USE ImportFromExcel;GOSELECT * INTO Data_ls FROM EXCELLINK...

Xem thêm: 8 Cách Tô Màu Ngày Chủ Nhật Trong Excel, Cách Lọc Ngày Thứ 7 Và Chủ Nhật Trong Excel

;GOYou can create a linked VPS from SQL Server Management Studio, or by running the system stored procedure sp_addlinkedserver, as shown in the following example.

DECLARE
RC intDECLARE
hệ thống nvarchar(128)DECLARE
srvproduct nvarchar(128)DECLARE
provider nvarchar(128)DECLARE
datasrc nvarchar(4000)DECLARE
location nvarchar(4000)DECLARE
provstr nvarchar(4000)DECLARE
catalog nvarchar(128)-- Set parameter valuesSET
hệ thống = "EXCELLINK"SET
srvproduct = "Excel"SET
provider = "vuialo.net.ACE.OLEDB.12.0"SET
datasrc = "C:TempData.xlsx"SET
provstr = "Excel 12.0"EXEC
RC = ..
VPS,
srvsản phẩm,
provider,
datasrc,
location,
provstr,
catalogFor more info about linked servers, see the following topics:

For more examples và info about both linked servers & distributed queries, see the following topics:

Prerequisite - Save Excel data as text

To use the rest of the methods described on this page - the BULK INSERT statement, the BCPhường tool, or Azure Data Factory - first you have sầu to lớn export your Excel data lớn a text file.

In Excel, select File | Save As and then select Text (Tab-delimited) (*.txt) or CSV (Comma-delimited) (*.csv) as the destination tệp tin type.

If you want lớn export multiple worksheets from the workbook, select each sheet and then repeat this procedure. The Save sầu as comm& exports only the active sheet.


Tip

For best results with data importing tools, save sheets that contain only the column headers and the rows of data. If the saved data contains page titles, blank lines, notes, và so forth, you may see unexpected results later when you import the data.


The Import Flat File Wizard

Import data saved as text files by stepping through the pages of the Import Flat File Wizard.

As described previously in the Prerequisite section, you have to export your Excel data as text before you can use the Import Flat File Wizard to import it.

For more info about the Import Flat File Wizard, see Import Flat File khổng lồ SQL Wizard.

BULK INSERT command

BULK INSERT is a Transact-SQL commvà that you can run from SQL Server Management Studio. The following example loads the data from the Data.csv comma-delimited file inlớn an existing database table.

As described previously in the Prerequisite section, you have sầu to export your Excel data as text before you can use BULK INSERT lớn import it. BULK INSERT can"t read Excel files directly. With the BULK INSERT commvà, you can import a CSV tệp tin that is stored locally or in Azure Blob storage.

USE ImportFromExcel;GOBULK INSERT Data_bi FROM "C:Tempdata.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = " ");GOFor more info & examples for SQL Server và Squốc lộ Database, see the following topics:

BCPhường tool

BCP.. is a program that you run from the commvà prompt. The following example loads the data from the Data.csv comma-delimited file into the existing Data_bcp database table.

As described previously in the Prerequisite section, you have khổng lồ export your Excel data as text before you can use BCPhường lớn import it. BCPhường can"t read Excel files directly. Use lớn import inkhổng lồ Squốc lộ Server or Squốc lộ Database from a kiểm tra (CSV) file saved khổng lồ local storage.


Important

For a text (CSV) tệp tin stored in Azure Blob storage, use BULK INSERT or OPENROWSET. For an examples, see Example.


bcp.exe ImportFromExcel..Data_bcp in "C:Tempdata.csv" -T -c -t ,For more info about BCPhường, see the following topics:

Copy Wizard (Azure Data Factory)

Import data saved as text files by stepping through the pages of the Azure Data Factory Copy Wizard.

As described previously in the Prerequisite section, you have sầu lớn export your Excel data as text before you can use Azure Data Factory lớn import it. Data Factory can"t read Excel files directly.

For more info about the Copy Wizard, see the following topics:

Azure Data Factory

If you"re familiar with Azure Data Factory và don"t want lớn run the Copy Wizard, create a pipeline with a Copy activity that copies from the text file to lớn SQL Server or to Azure SQL Database.

As described previously in the Prerequisite section, you have sầu to lớn export your Excel data as text before you can use Azure Data Factory khổng lồ import it. Data Factory can"t read Excel files directly.

For more info about using these Data Factory sources and sinks, see the following topics:

To start learning how to lớn copy data with Azure data factory, see the following topics:

Comtháng errors

vuialo.net.ACE.OLEDB.12.0" has not been registered

This error occurs because the OLEDB provider is not installed. Install it from vuialo.net Access Database Engine 2010 Redistributable. Be sure to lớn install the 64-bit version if Windows and SQL Server are both 64-bit.

The full error is:

Msg 7403, Level 16, State 1, Line 3The OLE DB provider "vuialo.net.ACE.OLEDB.12.0" has not been registered.

Cannot create an instance of OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked server "(null)"

This indicates that the vuialo.net OLEDB has not been configured properly. Run the following Transact-SQL code to lớn resolve this:

EXEC sp_MSset_oledb_prop N"vuialo.net.ACE.OLEDB.12.0", N"AllowInProcess", 1EXEC sp_MSset_oledb_prop N"vuialo.net.ACE.OLEDB.12.0", N"DynamicParameters", 1The full error is:

Msg 7302, Level 16, State 1, Line 3Cannot create an instance of OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked VPS "(null)".

The 32-bit OLE DB provider "vuialo.net.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit Squốc lộ Server

This occurs when a 32-bit version of the OLD DB provider is installed with a 64-bit Squốc lộ Server. To resolve this issue, uninstall the 32-bit version & install the 64-bit version of the OLE DB provider instead.

The full error is:

Msg 7438, Level 16, State 1, Line 3The 32-bit OLE DB provider "vuialo.net.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit Squốc lộ Server.

The OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked VPS "(null)" reported an error. The provider did not give any information about the error

Cannot initialize the data source object of OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked server "(null)"

Both of these errors typically indicate a permissions issue between the SQL Server process và the tệp tin. Ensure that the account that is running the SQL Server service has full access permission to the file. We recommend against trying to import files from the desktop.

Xem thêm: Hàm Liệt Kê Danh Sách Trong Excel, Tra Cứu Các Giá Trị Trong Danh Sách Dữ Liệu

The full errors are:

Msg 7399, Level 16, State 1, Line 3The OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give any information about the error.Msg 7303, Level 16, State 1, Line 3Cannot initialize the data source object of OLE DB provider "vuialo.net.ACE.OLEDB.12.0" for linked hệ thống "(null)".

See Also

Import data from Excel or export data to Excel with Squốc lộ Server Integration Services (SSIS)


Chuyên mục: Thủ Thuật