Hướng dẫn import file excel vào sql server

In this article

Applies to:

*
SQL Server (all supported versions)
*
Azure SQL Database

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

You watching: Hướng dẫn import file excel vào sql server

List of methods

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

Export to text first (Squốc lộ Server & Squốc lộ Database)Directly from Excel (SQL Server on-premises only)
Import Flat File WizardSquốc lộ Server Import và Export Wizard
BULK INSERT statementSQL Server Integration Services (SSIS)
BCPOPENROWSET function
Copy Wizard (Azure Data Factory)
Azure Data Factory
  

If you want to lớn import multiple worksheets from an Excel workbook, you typically have sầu lớn run any of these tools once for each sheet.

A complete mô tả tìm kiếm of complex tools & services like SSIS or Azure Data Factory is beyond the scope of this list. To learn more about the solution that interests you, follow the provided liên kết.


Important

For detailed info about connecting to Excel files, và about limitations & known issues for loading data from or khổng lồ Excel files, see Load data from or to lớn Excel with SQL Server Integration Services (SSIS).


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

Squốc lộ Server Import & Export Wizard

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

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

Exp& Databases.

Right-click a database.

Point to Tasks.

Cliông xã one of the following options.

Import Data

Export Data

*

*

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

To learn about other ways lớn launch the Import & Export wizard, see Start the Squốc lộ Server Import và Export Wizard.

SQL Server Integration Services (SSIS)

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

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

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

*

OPENROWSET & linked servers


Important

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


Note

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


Distributed queries

Import data directly into SQL Server from Excel files by using the Transact-SQL OPENROWSET or OPENDATASOURCE function. This usage is called a distributed query.


Important

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


Before you can run a distributed query, you have khổng lồ enable the ad hoc distributed queries hệ thống 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 to import the data from the Excel Sheet1 worksheet into a new database table.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENROWSET("peaceworld.com.vn.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("peaceworld.com.vn.ACE.OLEDB.12.0", "Data Source=C:TempData.xlsx;Extended Properties=Excel 12.0")...;GOTo append the imported data 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 SQL Server to lớn the Excel file as a linked server. The following example imports the data from the Data worksheet on the existing Excel linked hệ thống EXCELLINK into lớn a new SQL Server database table named Data_ls.

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

See more: Tag: Bạch Bách Hà - Bạch Bách Hà Và Top 15 Phim Hay Nhất

;GOYou can create a linked hệ thống 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
srvhàng hóa 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
srvsản phẩm = "Excel"SET
provider = "peaceworld.com.vn.ACE.OLEDB.12.0"SET
datasrc = "C:TempData.xlsx"SET
provstr = "Excel 12.0"EXEC
RC = ..
hệ thống,
srvsản phẩm,
provider,
datasrc,
location,
provstr,
catalogFor more info about linked servers, see the following topics:

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

Prerequisite - Save sầu 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 to lớn export your Excel data to a text tệp tin.

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

If you want to export multiple worksheets from the workbook, select each sheet and then repeat this procedure. The Save sầu as commvà exports only the active sheet.


Tip

For best results with data importing tools, save sầu sheets that contain only the column headers và the rows of data. If the saved data contains page titles, blank lines, notes, and 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 lớn export your Excel data as text before you can use the Import Flat File Wizard to lớn import it.

For more info about the Import Flat File Wizard, see Import Flat File to lớn Squốc lộ Wizard.

BULK INSERT command

BULK INSERT is a Transact-Squốc lộ command that you can run from Squốc lộ Server Management Studio. The following example loads the data from the Data.csv comma-delimited tệp tin inlớn an existing database table.

As described previously in the Prerequisite section, you have sầu khổng lồ export your Excel data as text before you can use BULK INSERT khổng lồ 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 và examples for Squốc lộ Server and SQL Database, see the following topics:

BCPhường. tool

BCP is a program that you run from the command prompt. The following example loads the data from the Data.csv comma-delimited tệp tin into the existing Data_bcp database table.

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


Important

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


bcp.exe cộ ImportFromExcel..Data_bcp in "C:Tempdata.csv" -T -c -t ,For more info about BCP.., 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 to lớn export your Excel data as text before you can use Azure Data Factory to 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 khổng lồ run the Copy Wizard, create a pipeline with a Copy activity that copies from the text file to SQL Server or to Azure Squốc lộ 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 lớn import it. Data Factory can"t read Excel files directly.

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

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

Common errors

peaceworld.com.vn.ACE.OLEDB.12.0" has not been registered

This error occurs because the OLEDB provider is not installed. Install it from peaceworld.com.vn Access Database Engine 2010 Redistributable. Be sure to install the 64-bit version if Windows & Squốc lộ Server are both 64-bit.

The full error is:

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

Cannot create an instance of OLE DB provider "peaceworld.com.vn.ACE.OLEDB.12.0" for linked VPS "(null)"

This indicates that the peaceworld.com.vn OLEDB has not been configured properly. Run the following Transact-SQL code lớn resolve sầu this:

EXEC sp_MSset_oledb_prop N"peaceworld.com.vn.ACE.OLEDB.12.0", N"AllowInProcess", 1EXEC sp_MSset_oledb_prop N"peaceworld.com.vn.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 "peaceworld.com.vn.ACE.OLEDB.12.0" for linked hệ thống "(null)".

The 32-bit OLE DB provider "peaceworld.com.vn.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 and 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 "peaceworld.com.vn.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit SQL Server.

The OLE DB provider "peaceworld.com.vn.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 "peaceworld.com.vn.ACE.OLEDB.12.0" for linked VPS "(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 Squốc lộ Server service has full access permission lớn the tệp tin. We recommend against trying to lớn import files from the desktop.

See more: Các Cách Chỉnh Âm Thanh Máy Tính, Cách Để Sửa Lỗi Âm Thanh Máy Tính Windows

The full errors are:

Msg 7399, Level 16, State 1, Line 3The OLE DB provider "peaceworld.com.vn.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give sầu any information about the error.Msg 7303, Level 16, State 1, Line 3Cannot initialize the data source object of OLE DB provider "peaceworld.com.vn.ACE.OLEDB.12.0" for linked VPS "(null)".

See Also

Import data from Excel or export data khổng lồ Excel with Squốc lộ Server Integration Services (SSIS)