|
1 | | -# Excel2sql_create_table |
| 1 | +# Excel to SQL CREATE TABLE PowerShell Script |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This project provides a PowerShell script (`ExcelToSqlCreateTable.ps1`) to automatically generate a SQL `CREATE 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. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Automatic column type detection:** Detects `INT`, `FLOAT`, `DATETIME`, or `NVARCHAR` types based on column content. |
| 10 | +- **Handles duplicate and unnamed columns:** Ensures all column names are unique, even if the Excel sheet contains duplicates or blanks. |
| 11 | +- **Multi-file support:** |
| 12 | + - If multiple `.xlsx` files are present in the script directory, the script lists them and prompts you to select which one to use. |
| 13 | + - If only one `.xlsx` file is present, it is selected automatically. |
| 14 | +- **User-friendly prompts:** Asks for sheet name, SQL table name, output file name, and type detection threshold. |
| 15 | +- **Output preview:** Displays the generated SQL statement in the console as well as saving it to a file. |
| 16 | +- **Robust error handling:** Provides clear error messages for missing files, unreadable sheets, or missing columns. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- **PowerShell:** The script is designed for Windows PowerShell (tested on 5.1+) and PowerShell Core. |
| 21 | +- **ImportExcel module:** The script uses the [ImportExcel](https://github.com/dfinke/ImportExcel) PowerShell module. |
| 22 | + - If not present, the script will attempt to install it automatically. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +1. **Clone or download** this repository to your local machine. |
| 27 | +2. Place your Excel `.xlsx` files in the same directory as the script. |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +1. **Open PowerShell** and navigate to the directory containing the script and your Excel file(s). |
| 32 | + |
| 33 | +2. **Run the script:** |
| 34 | + |
| 35 | + ```powershell |
| 36 | + .\ExcelToSqlCreateTable.ps1 |
| 37 | + ``` |
| 38 | +
|
| 39 | +3. **If multiple `.xlsx` files are present:** |
| 40 | + - The script will display a numbered list of all `.xlsx` files in the directory. |
| 41 | + - Enter the corresponding number to select your desired file. |
| 42 | +
|
| 43 | +4. **Follow the prompts:** |
| 44 | + - **Sheet name:** Enter the name of the Excel worksheet (not the file or table name). |
| 45 | + - **SQL table name:** Enter the desired name for the SQL table. |
| 46 | + - **Output file name:** Enter the output file for the SQL script (press Enter to use the default). |
| 47 | + - **Type detection threshold:** Enter a number (press Enter to use the default, usually 500). |
| 48 | + - The script will display detected columns and types, and create an output file with the SQL command. |
| 49 | +
|
| 50 | +5. **Check the output:** |
| 51 | + - The generated SQL statement is shown in the console and saved to the output file you specified. |
| 52 | +
|
| 53 | +## Example |
| 54 | +
|
| 55 | +Suppose you have two Excel files, `data1.xlsx` and `data2.xlsx`, in the same folder as the script. When you run: |
| 56 | +
|
| 57 | +```powershell |
| 58 | +.\ExcelToSqlCreateTable.ps1 |
| 59 | +``` |
| 60 | + |
| 61 | +You will see output like: |
| 62 | + |
| 63 | +``` |
| 64 | +Multiple .xlsx files found in the folder: |
| 65 | +[0] data1.xlsx |
| 66 | +[1] data2.xlsx |
| 67 | +Enter the number of the Excel file to use: |
| 68 | +``` |
| 69 | +Enter the number corresponding to the file you want to process, and continue as prompted. |
| 70 | + |
| 71 | +## Troubleshooting |
| 72 | + |
| 73 | +- **No Excel file found:** Ensure at least one `.xlsx` file is in the script directory. |
| 74 | +- **Module ImportExcel not found:** The script will try to install it. If you encounter permission issues, install manually: |
| 75 | + ```powershell |
| 76 | + Install-Module -Name ImportExcel -Scope CurrentUser |
| 77 | + ``` |
| 78 | +- **Sheet not found:** Double-check the worksheet name is correct (case-sensitive). |
| 79 | +- **Weird column names:** If your Excel has empty or duplicate headers, the script will auto-correct them (`UnnamedColumn`, `ColumnName_2`, etc.). |
| 80 | +
|
| 81 | +## Customization |
| 82 | +
|
| 83 | +- You can modify the script to change the default output file name, adjust SQL type mapping, or add more logic for custom data types. |
| 84 | +
|
| 85 | +## License |
| 86 | +
|
| 87 | +MIT License. See [LICENSE](LICENSE) for details. |
| 88 | +
|
| 89 | +## Credits |
| 90 | +
|
| 91 | +Based on PowerShell scripting and the ImportExcel module by Doug Finke. |
0 commit comments