Exporting Excel file to SQL server database
Key features of this script:
- Supports both row-by-row and batch import modes
- Includes error handling and validation
- Progress reporting during import
- Configuration management using JSON file
- Command-line interface for easy usage
- Supports any Excel file structure (automatically uses column names)
Important notes:
- Ensure your SQL Server table structure matches your Excel file columns
- The script assumes the first row of your Excel file contains column headers
- For large files, use batch mode (-b flag) for better performance
- Make sure you have appropriate permissions on the SQL Server database
- Store sensitive database credentials securely in the config file
The script includes progress logging (log.txt) and error handling to help you track the import process and troubleshoot any issues that might arise.
Prerequisite: Install latest Python 3.12.x
Setup:
-
Open config.json file. Change the database connection and user authentication credentials.
-
Open Command Prompt/PowerShell/Terminal. Change directory to the project folder
cd path_to_project_folder/import_excel_sql
-
Create Python virtual environment.
python -m venv .venv
-
Activating virtual environment.
source .venv/Scripts/activate
-
To install dependencies.
pip install -r requirements.txt
-
Executing the script via Command Prompt/PowerShell/Terminal to start importing.
Arguments:
-e | --excel : Path location of excel file tobe imported to SQL Server.
-t | --table : Target table name where to be inserted.
-b | --batch : Enable batch mode upload.
a. Importing by row.
python import_excel_sql.py -e ./sample/persons.xlsx -t persons
b. Importing by batch
python import_excel_sql.py -e ./sample/persons.xlsx -t persons -b