如何用SQL将多工作表Excel文件导入数据库为独立表?是否需手动转CSV?
Hey there! Great question—you absolutely don’t need to jump through the hoops of converting to CSV manually to import multi-sheet XLS files into a database. Let’s break down the most common, efficient approaches based on the database system you’re using:
1. Use Built-in Database Tools/Extensions (No Manual CSV Conversion Needed)
Most modern databases support direct Excel file ingestion via extensions, ODBC connections, or native functions. Here’s how to do it for the big three:
MySQL
You can leverage ODBC connectivity to treat your XLS file as a data source, then pull data directly into your tables:
- First, install the Microsoft Excel ODBC Driver (matches your Excel version) and configure your XLS file as an ODBC data source.
- Create target tables in MySQL that match the column structure and data types of each Excel worksheet.
- Use
INSERT INTO ... SELECTwithOPENROWSETto import each sheet:
Just replaceINSERT INTO your_target_table SELECT * FROM OPENROWSET( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:/path/to/your/file.xls', 'SELECT * FROM [Sheet1$]' );[Sheet1$]with the name of each worksheet (don’t forget the$suffix) and adjust the target table name for each sheet.
PostgreSQL
PostgreSQL has a handy pg_read_excel extension that lets you read Excel files directly:
- Install the extension first (you’ll need superuser privileges):
CREATE EXTENSION IF NOT EXISTS pg_read_excel; - Create your target tables, then import each sheet with:
Alternatively, if you prefer usingINSERT INTO your_target_table SELECT * FROM pg_read_excel('/path/to/your/file.xls', 'Sheet1');COPY, you can use a utility likexlscatto extract sheet data and pipe it into PostgreSQL:COPY your_target_table FROM PROGRAM 'xlscat -s "Sheet1" /path/to/your/file.xls' WITH (FORMAT CSV);
SQL Server
SQL Server has native support for reading Excel files via OPENROWSET or OPENDATASOURCE:
- First, enable Ad Hoc Distributed Queries (if not already enabled):
sp_configure 'show advanced options', 1; RECONFIGURE; sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE; - Import each worksheet into your target table:
INSERT INTO dbo.your_target_table SELECT * FROM OPENROWSET( 'Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;Database=C:/path/to/your/file.xls', 'SELECT * FROM [Sheet1$]' );HDR=YEStells the driver that the first row of your worksheet is column headers; useHDR=NOif all rows are data.
2. ETL Tools for Bulk/Complex Scenarios
If you have dozens of worksheets, need to clean data during import, or want to automate the process, ETL tools like Talend, Apache NiFi, or even Excel’s own Power Query are perfect. These tools automatically detect all worksheets in your XLS file, let you map columns to database tables, and push data directly—no CSV conversion required.
Do You Need to Convert to CSV?
Short answer: No! Manual CSV conversion is an unnecessary extra step that introduces risks like encoding errors, formatting loss, or missed data. The only time you might need it is if your database environment is extremely restricted (e.g., no ODBC driver access, no permission to install extensions). Otherwise, stick to the direct methods above.
内容的提问来源于stack exchange,提问作者phoez

