
Image retained from the original article.
Originally published 26 July 2023; rewritten and reviewed on 15 October 2026.
Almost every client project eventually drops an Excel workbook in your lap. Maybe it is a price list in RM from a finance team, a member directory exported from an old HR system, or 40,000 legacy customer rows that need to land in MySQL before a launch. Spreadsheets feel forgiving when people type into them, which is exactly why they are dangerous when fed straight into a database: one sheet hides inconsistent DD/MM/YYYY and YYYY-MM-DD dates, stripped leading zeros on Malaysian mobile numbers (012 turned into 12), merged cells, and trailing spaces.
MySQL’s LOAD DATA INFILE is built for speed, not for guessing what someone meant in an .xls or .xlsx file. Trying to force binary Excel files directly into SQL usually ends in brittle hacks. A much calmer approach is converting the sheet to CSV and passing it through a staging table where every assumption can be checked before touching production tables.
Convert the workbook to a clean CSV first
LOAD DATA only reads delimited text. Before touching MySQL, export the target tab from Excel as CSV UTF-8 (Comma delimited), or convert it reproducibly on the command line if you have multiple files to process:
libreoffice --headless --convert-to csv --outdir ./staging customers.xlsx
If the workbook has multiple tabs or formulas that behave strangely during conversion, a short Python script using openpyxl or pandas gives you deterministic control over which sheet gets exported and how empty cells are represented. Keep the original .xlsx untouched in an archive folder so that when someone asks why row 1,482 was rejected, you can point back to the exact cell.
Before running any SQL, inspect the first few lines of the CSV with head -n 5 customers.csv and file -i customers.csv. Two things trip people up constantly:
- Line endings: Files saved on Windows Excel almost always end lines with
\r\n(CRLF), whereas Linux tools produce\n(LF). If you tell MySQLLINES TERMINATED BY '\n'on a CRLF file, the last column of every row quietly keeps a trailing\rcharacter. - Byte Order Mark (BOM): Excel’s UTF-8 export sometimes prepends a
\xEF\xBB\xBFBOM to the very first header name. Stripping it or ignoring the header line withIGNORE 1 LINESsaves you from confusing column-mismatch errors.
Stage everything as text before casting types
Never LOAD DATA directly into your live application tables. Create a temporary or staging table where columns are forgiving VARCHAR fields, and let MySQL’s strict mode stay enabled on the production tables.
CREATE TABLE customer_import_stage (
row_num INT AUTO_INCREMENT PRIMARY KEY,
customer_code VARCHAR(50),
customer_name VARCHAR(255),
phone_raw VARCHAR(50),
email VARCHAR(255),
joined_date_raw VARCHAR(50)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
From there, you can map the CSV columns and even transform fields on the fly using user variables (@var) inside LOAD DATA INFILE:
LOAD DATA INFILE '/var/lib/mysql-files/customers.csv'
INTO TABLE customer_import_stage
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(@code, @name, @phone, @email, @joined)
SET
customer_code = NULLIF(TRIM(@code), ''),
customer_name = NULLIF(TRIM(@name), ''),
phone_raw = NULLIF(TRIM(@phone), ''),
email = LOWER(NULLIF(TRIM(@email), '')),
joined_date_raw = NULLIF(TRIM(@joined), '');
Notice how TRIM and NULLIF(..., '') immediately clean up empty cells so they become real SQL NULL values instead of zero-length strings. Once the data is in customer_import_stage, you can run ordinary SELECT queries to spot bad emails, duplicate customer_code entries, or Malaysian phone numbers missing their leading 0 before a single production row is written.
Understand secure_file_priv and LOCAL INFILE
If you run LOAD DATA INFILE and hit ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option, MySQL is doing its job. Check where the server permits file reads by running:
SHOW VARIABLES LIKE 'secure_file_priv';
On Ubuntu and Debian servers, this usually points to /var/lib/mysql-files/. Move your CSV there and verify that the mysql OS user has read permission on the file.
If you are importing from a remote workstation or managed database (like AWS RDS or DigitalOcean Managed MySQL) where you have no shell access to /var/lib/mysql-files/, you will need LOAD DATA LOCAL INFILE. That shifts file reading to the client process, which means both the server (local_infile = ON) and your client (mysql --local-infile=1) must explicitly opt in. Enable it only for the import session rather than leaving it turned on permanently in my.cnf for every application user.
Validate, promote, and reconcile the batch
With the staging table populated, run a quick health check before promoting rows:
-- Check total staged rows and obvious anomalies
SELECT
COUNT(*) AS total_rows,
SUM(customer_code IS NULL) AS missing_codes,
SUM(email NOT LIKE '%@%.%') AS suspect_emails,
SUM(STR_TO_DATE(joined_date_raw, '%d/%m/%Y') IS NULL AND joined_date_raw IS NOT NULL) AS bad_dates
FROM customer_import_stage;
Once the anomalies are fixed in the source or filtered out explicitly, promote the batch inside a transaction so it either lands completely or rolls back cleanly:
START TRANSACTION;
INSERT INTO customers (customer_code, customer_name, phone, email, joined_at)
SELECT
customer_code,
customer_name,
phone_raw,
email,
STR_TO_DATE(joined_date_raw, '%d/%m/%Y')
FROM customer_import_stage
WHERE customer_code IS NOT NULL
ON DUPLICATE KEY UPDATE
customer_name = VALUES(customer_name),
phone = VALUES(phone),
email = VALUES(email);
COMMIT;
After committing, verify SELECT COUNT(*) FROM customers; against your expected delta, spot-check a handful of names with Unicode characters and leading zeros, and then drop or archive customer_import_stage. A fast import saves ten minutes; a reconciled staging import saves an entire weekend of cleaning up corrupted production records.