DEV Community

Jason
Jason

Posted on

Excel and CSV Import to Database: Encoding, Column Mapping, and Batch SQL Inserts

Business teams send Excel files.

Operations export CSVs.

Clients email spreadsheets.

And somehow it's your job to get everything into a database without breaking production.

Whether you're importing into SQL Server, MySQL, PostgreSQL, Oracle, or SQLite, the same problems appear again and again:

Garbled characters
Incorrect column mapping
Data type errors
Duplicate records
Huge INSERT scripts that timeout

This guide walks through the complete spreadsheet β†’ database workflow and highlights the mistakes that cause most failed imports.

If you don't want to write SQL manually, you can use this free online Excel to SQL Generator:

πŸ‘‰ https://comtools.cn/Tools/Excel/ToSql

It supports generating INSERT, UPDATE, DELETE, MERGE, and SELECT statements directly from Excel or CSV files.

Typical Import Workflow

Most database imports follow roughly this process:

Receive an Excel or CSV file
Clean the data
Generate SQL
Test on a staging database
Execute in production in batches
Verify imported records

Skipping the cleaning or testing phase is where most problems begin.

Supported File Formats
Format Supported Notes
.xlsx βœ… Recommended
.xlsm βœ… Supported
.xltx βœ… Supported
.xltm βœ… Supported
.csv βœ… UTF-8 recommended
.xls ❌ Convert to .xlsx first

For Excel files:

Row 1 should contain column names.
Data should begin on Row 2.
Avoid merged cells.
Remove blank header rows.
CSV Encoding Problems

One of the most common import issues has nothing to do with SQL.

It's encoding.

If your CSV isn't UTF-8, you'll often see corrupted text after import.

Problem Cause Solution
Chinese characters become garbled GBK / GB2312 encoding Save as CSV UTF-8
First column looks strange UTF-8 BOM Usually harmless
Columns shift unexpectedly Commas inside text Quote values or save as Excel

A good rule is simple:

Always standardize CSV files to UTF-8 before importing.

Column Mapping Best Practices

Life becomes much easier if spreadsheet headers match your database columns.

Instead of:

User Name

use:

UserName

Instead of:

Order Number

use:

OrderNumber

Also remove columns that don't belong in the database:

Notes
Comments
Row numbers
Temporary calculations
Helper columns

Only keep fields that actually exist in your table.

Data Types Matter

Every spreadsheet value eventually becomes SQL.

Spreadsheet Value SQL Output
Text 'Text'
Integer 123
Decimal 12.56
Empty Cell NULL
Date '2026-07-02'
Single Quote Escaped as ''

Typical problems include:

"N/A" inserted into an INT column
Different regional date formats
Currency symbols inside numeric fields

Whenever possible, standardize dates as:

yyyy-MM-dd

before generating SQL.

Which SQL Statement Should You Generate?

Different jobs require different SQL.

Goal SQL
Import new records INSERT
Modify existing rows UPDATE
Delete records DELETE
Verify existing IDs SELECT

If you're looking for an online generator, try:

πŸ‘‰ https://comtools.cn/Tools/Excel/ToSql

It supports:

INSERT
UPDATE
DELETE
MERGE
SELECT
SQL Server
MySQL
PostgreSQL
Oracle
SQLite

along with batch SQL generation for large datasets.

Handling Large Imports

Large imports deserve special treatment.

Instead of generating one enormous SQL script:

Split files larger than 50,000 rows
Download SQL instead of copying huge browser output
Execute batches of 500–1000 rows
Verify row counts after each batch

For SQL Server, batching reduces:

Transaction log growth
Lock duration
Execution time
Memory usage

Many SQL generators combine rows into:

INSERT INTO Users
VALUES
(...),
(...),
(...);

which performs significantly better than executing thousands of individual INSERT statements.

Clean Your Data First

Cleaning usually takes minutes.

Fixing production may take hours.

Before generating SQL:

Remove duplicate emails
Remove duplicate order IDs
Normalize dates
Trim spaces
Remove hidden rows
Remove unnecessary columns

If multiple Excel files contain related data, merge them before generating SQL.

Multiple Database Engines

Most modern SQL generators support:

SQL Server
MySQL
PostgreSQL
SQLite
Oracle

Keep in mind that SQL syntax isn't identical.

Identifier quoting, batch syntax, and certain functions differ between database engines.

Always generate SQL specifically for your target database.

Privacy and Security

Spreadsheet imports often contain customer information.

Recommended practices:

Use anonymized data whenever possible
Test in a staging environment first
Keep production backups
Avoid storing uploaded files permanently
Import Checklist

Before running any generated SQL, verify:

βœ… CSV uses UTF-8 encoding
βœ… Headers match database columns
βœ… Data types are correct
βœ… Dates are standardized
βœ… Duplicate records removed
βœ… Tested on staging
βœ… Production backup completed
βœ… SQL executed in batches
βœ… Row counts verified
βœ… Business users confirmed imported data
Try the Free Excel to SQL Generator

If you regularly import Excel or CSV files into databases, you can save a lot of time by generating SQL automatically.

πŸ‘‰ https://comtools.cn/Tools/Excel/ToSql

Features include:

βœ… Excel (.xlsx) support
βœ… CSV support
βœ… INSERT generation
βœ… UPDATE generation
βœ… DELETE generation
βœ… MERGE generation
βœ… SELECT (WHERE IN) generation
βœ… Batch INSERT
βœ… SQL Server
βœ… MySQL
βœ… PostgreSQL
βœ… Oracle
βœ… SQLite

No installation is requiredβ€”just upload your spreadsheet, configure the options, and generate SQL in seconds.

Final Thoughts

Most failed database imports aren't caused by SQL.

They're caused by inconsistent spreadsheets.

Encoding issues, mismatched column names, duplicate records, and incorrect data types account for the majority of import failures.

Building a repeatable import workflowβ€”and using tools that automatically generate SQL from Excel or CSVβ€”can save hours of manual work while reducing production errors.

If you frequently work with spreadsheets and databases, keeping an Excel to SQL Generator in your toolbox is one of the easiest ways to speed up imports and improve reliability.

Top comments (0)