Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
PowerShell script to generate a SQL CREATE TABLE statement from Excel files, with automatic column type detection and header handling.
License
R0mb0/Excel_to_SQL_Create_Table
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project provides a PowerShell script (ExcelToSqlCreateTable.ps1) to automatically generate a SQLCREATE TABLE statement from the structure and content of an Excel.xlsx file. The script analyzes the sheet's headers and data, deduces the most appropriate SQL data types for each column, and handles common Excel issues such as duplicate headers, unnamed columns, and empty cells.
- Automatic column type detection: Detects
INT,FLOAT,DATETIME, orNVARCHARtypes based on column content. - Handles duplicate and unnamed columns: Ensures all column names are unique, even if the Excel sheet contains duplicates or blanks.
- Multi-file support:
- If multiple
.xlsxfiles are present in the script directory, the script lists them and prompts you to select which one to use. - If only one
.xlsxfile is present, it is selected automatically.
- If multiple
- User-friendly prompts: Asks for sheet name, SQL table name, output file name, and type detection threshold.
- Output preview: Displays the generated SQL statement in the console as well as saving it to a file.
- Robust error handling: Provides clear error messages for missing files, unreadable sheets, or missing columns.
- PowerShell: The script is designed for Windows PowerShell (tested on 5.1+) and PowerShell Core.
- ImportExcel module: The script uses theImportExcel PowerShell module.
- If not present, the script will attempt to install it automatically.
- Clone or download this repository to your local machine.
- Place your Excel
.xlsxfiles in the same directory as the script.
Open PowerShell and navigate to the directory containing the script and your Excel file(s).
Run the script:
.\ExcelToSqlCreateTable.ps1
If the script doesn't run, please execute this command, then run the script again.
Set-ExecutionPolicy-ScopeProcess-ExecutionPolicy Bypass
If multiple
.xlsxfiles are present:- The script will display a numbered list of all
.xlsxfiles in the directory. - Enter the corresponding number to select your desired file.
- The script will display a numbered list of all
Follow the prompts:
- Sheet name: Enter the name of the Excel worksheet (not the file or table name).
- SQL table name: Enter the desired name for the SQL table.
- Output file name: Enter the output file for the SQL script (press Enter to use the default).
- Type detection threshold: Enter a number (press Enter to use the default, usually 500).
- The script will display detected columns and types, and create an output file with the SQL command.
Check the output:
- The generated SQL statement is shown in the console and saved to the output file you specified.
Suppose you have two Excel files,data1.xlsx anddata2.xlsx, in the same folder as the script. When you run:
.\ExcelToSqlCreateTable.ps1
You will see output like:
Multiple .xlsx files found in the folder:[0] data1.xlsx[1] data2.xlsxEnter the number of the Excel file to use:Enter the number corresponding to the file you want to process, and continue as prompted.
- No Excel file found: Ensure at least one
.xlsxfile is in the script directory. - Module ImportExcel not found: The script will try to install it. If you encounter permission issues, install manually:
Install-Module-Name ImportExcel-Scope CurrentUser
- Sheet not found: Double-check the worksheet name is correct (case-sensitive).
- Weird column names: If your Excel has empty or duplicate headers, the script will auto-correct them (
UnnamedColumn,ColumnName_2, etc.).
MIT License. SeeLICENSE for details.
Based on PowerShell scripting and the ImportExcel module by Doug Finke.
About
PowerShell script to generate a SQL CREATE TABLE statement from Excel files, with automatic column type detection and header handling.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.