101

Can any one explain how to import a Microsoft Excel file in to a MySQL database?

For example, my Excel table looks like this:

Country   |   Amount   |   Qty
----------------------------------
America   |   93       |   0.60

Greece    |   9377     |   0.80

Australia |   9375     |   0.80
12
  • [You might take a look at this thread][1] on the MySQL forums. It explains how to do exactly what you want. [1]:forums.mysql.com/read.php?32,216343,216344#msg-216344
    – Kredns
    Aug 21, 2009 at 5:11
  • 1
    This free utility makes importing excel spreadsheets into mysql tables quick and easy panofish.net/…
    – panofish
    Jun 13, 2013 at 20:06
  • +1 for a new and different idea. The processing is insanely slow, it first reads each row in file and then upload anything. It took about 15 mins to import 5.2K rows
    – Fr0zenFyr
    Sep 20, 2013 at 11:27
  • For some spreadsheets it can be slow, due to number of columns and rows. But, I blame microsofts com-object library which is what is used to read the spreadsheet. My program reads the spreadsheet as fast as the library will allow. The mysql inserts are very fast. If you can eliminate unnecessary columns before importing... that can help.
    – panofish
    Jan 27, 2014 at 21:10
  • I had 14 columns on 5173 rows. I had already selected all the empty columns/rows and deleted them to avoid unnecessary processing. Sytem that I was using had 2.5G RAM and core2duo processor, didn't have many processes running, performance tab showed 65% usage in all and a lot of RAM still unused. So, I guess I wouldn't blame the hardware but like you said, MS com objects suck.. I don't know when MS will stop building crap that look like life savers for novices. I'm sick of doing extra crap for MS products.
    – Fr0zenFyr
    Jan 28, 2014 at 5:02

15 Answers 15

130

There's a simple online tool that can do this called sqlizer.io.

Screenshot from sqlizer.com

You upload an XLSX file to it, enter a sheet name and cell range, and it will generate a CREATE TABLE statement and a bunch of INSERT statements to import all your data into a MySQL database.

(Disclaimer: I help run SQLizer)

11
  • 32
    Good to know this tool exists, but definitely think about if you should use it or not. If your spreadsheet contains sensitive data (e.g. user emails, passwords, cc info, medical info, etc) it may not be a good idea. This site may not store your data and it may be secure, but there's no way for you to know that for sure. Aug 2, 2016 at 15:46
  • 1
    @DivyeshJesadiya It's free for anything up to 5000 rows. After that you have to pay $10 for a month of usage.
    – d4nt
    Dec 20, 2016 at 16:24
  • @doxsi it seems fine to me, what sort of problem did you have with it?
    – d4nt
    Mar 21, 2017 at 10:41
  • @ChrisSchmitz we are super-cautious with security and wrote a blog post recently about how we operate SQLizer blog.sqlizer.io/posts/privacy-at-sqlizer Mar 28, 2017 at 15:52
  • It works, pay attention at the Excel format, xlt isn't accepted meanwhile xls is accepted. Apr 5, 2018 at 10:19
52

Below is another method to import spreadsheet data into a MySQL database that doesn't rely on any extra software. Let's assume you want to import your Excel table into the sales table of a MySQL database named mydatabase.

  1. Select the relevant cells:

    enter image description here

  2. Paste into Mr. Data Converter and select the output as MySQL:

    enter image description here

  3. Change the table name and column definitions to fit your requirements in the generated output:

CREATE TABLE sales (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  Country VARCHAR(255),
  Amount INT,
  Qty FLOAT
);
INSERT INTO sales
  (Country,Amount,Qty)
VALUES
  ('America',93,0.60),
  ('Greece',9377,0.80),
  ('Australia',9375,0.80);
  1. If you're using MySQL Workbench or already logged into mysql from the command line, then you can execute the generated SQL statements from step 3 directly. Otherwise, paste the code into a text file (e.g., import.sql) and execute this command from a Unix shell:

    mysql mydatabase < import.sql

    Other ways to import from a SQL file can be found in this Stack Overflow answer.

2
  • 1
    This was the easiest way. Thanks! Just remember to remove the extra Primary Key 'ID' in the SQL, and place an AUTO increment of your choice.
    – Fandango68
    Jul 26, 2019 at 1:09
  • That's awesome, by far the easiest solution. Great find! Feb 25, 2021 at 10:05
50
  1. Export it into some text format. The easiest will probably be a tab-delimited version, but CSV can work as well.

  2. Use the load data capability. See http://dev.mysql.com/doc/refman/5.1/en/load-data.html

  3. Look half way down the page, as it will gives a good example for tab separated data:

    FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\'

  4. Check your data. Sometimes quoting or escaping has problems, and you need to adjust your source, import command-- or it may just be easier to post-process via SQL.

6
  • 2
    When exporting to CSV, [at least] excel 2013 actively attempts to poison data by using VBA-escaped doublequotes, using locale-dependent (based on OS regional settings) decimal separator for 0 values (e.g. ',', while using 'as defined in cell properties' separator for all other values. Best to stay away from CSV.
    – afk5min
    Apr 12, 2014 at 15:14
  • 1
    Note that this procedure requires that you first create the table, with the appropriate fields.
    – LarsH
    Mar 24, 2016 at 15:00
  • That's isn't the only problem. I am not sure if this procedure deals correctly with carriage return inside a cell in excel. Even the imports from csv to excel fails in that
    – Raul Luna
    Apr 12, 2016 at 10:35
  • 1
    @afk5min what do you mean by "poison data"??? All those tags and markup are very useful for the users that are going to obviously import that into another MS product...
    – Mindwin
    Oct 24, 2017 at 14:06
  • 1
    *You meant "The hardest way" not the easiest
    – Revious
    Jul 8, 2019 at 16:19
29

There are actually several ways to import an excel file in to a MySQL database with varying degrees of complexity and success.

  1. Excel2MySQL. Hands down, the easiest and fastest way to import Excel data into MySQL. It supports all verions of Excel and doesn't require Office install.

    screenshot of Excel2MySQL

  2. LOAD DATA INFILE: This popular option is perhaps the most technical and requires some understanding of MySQL command execution. You must manually create your table before loading and use appropriately sized VARCHAR field types. Therefore, your field data types are not optimized. LOAD DATA INFILE has trouble importing large files that exceed 'max_allowed_packet' size. Special attention is required to avoid problems importing special characters and foreign unicode characters. Here is a recent example I used to import a csv file named test.csv.

    enter image description here

  3. phpMyAdmin: Select your database first, then select the Import tab. phpMyAdmin will automatically create your table and size your VARCHAR fields, but it won't optimize the field types. phpMyAdmin has trouble importing large files that exceed 'max_allowed_packet' size.

    enter image description here

  4. MySQL for Excel: This is a free Excel Add-in from Oracle. This option is a bit tedious because it uses a wizard and the import is slow and buggy with large files, but this may be a good option for small files with VARCHAR data. Fields are not optimized.

    enter image description here

5
  • Hi @panofish, I have a excel which gets updated daily. I would like to put only the updated data to be put to the mysql database table rather than the whole table. (i.e) put one row into the table per day. How can this be done?
    – Arun Raja
    Nov 24, 2014 at 7:35
  • These solutions all focus on loading an entire spreadsheet and replacing your MySQL table or appending to the table. Are the changes adding new records or changing existing records?
    – panofish
    Nov 24, 2014 at 14:20
  • They are just overwriting the values. But I want only those values to be appended which are not already present in the database based on the date. This can increase the performance by just adding some rows instead of adding many rows daily.
    – Arun Raja
    Nov 25, 2014 at 2:34
  • 2
    Excel2MySQL tool should have the possibility to only create the database script :(
    – joseantgv
    Jul 29, 2015 at 14:00
  • PHPMyAdmin was the easiest and quickest one. Sep 7, 2018 at 15:10
2

Not sure if you have all this setup, but for me I am using PHP and MYSQL. So I use a PHP class PHPExcel. This takes a file in nearly any format, xls, xlsx, cvs,... and then lets you read and / or insert.

So what I wind up doing is loading the excel in to a phpexcel object and then loop through all the rows. Based on what I want, I write a simple SQL insert command to insert the data in the excel file into my table.

On the front end it is a little work, but its just a matter of tweaking some of the existing code examples. But when you have it dialed in making changes to the import is simple and fast.

1
  • PHPExcel last version, 1.8.1, was released in 2015. The project was officially deprecated in 2017 and permanently archived in 2019
    – Wondarar
    Oct 15, 2021 at 15:53
1

the best and easiest way is to use "MySQL for Excel" app that is a free app from oracle. this app added a plugin to excel to export and import data to mysql. you can download that from here

1

When using text files to import data, I had problems with quotes and how Excel was formatting numbers. For example, my Excel configuration used the comma as decimal separator instead of the dot.

Now I use Microsoft Access 2010 to open my MySql table as linked table. There I can simply copy and paste cells from Excel to Access.

To do this, first install the MySql ODBC driver and create an ODBC connection. Then in access, in the "External Data" tab, open "ODBC Database" dialog and link to any table using the ODBC connection.

Using MySql Workbench, you can also copy and paste your Excel data into the result grid of MySql Workbench. I gave detailed instructions in this answer.

1

Fastest and simpliest way is to save XLS as ODS (open document spreasheet) and import it from PhpMyAdmin enter image description here

0

For a step by step example for importing Excel 2007 into MySQL with correct encoding (UTF-8) search for this comment:

"Posted by Mike Laird on October 13 2010 12:50am"

in the next URL:

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

0

You could use DocChow, a very intuitive GIU for importing Excel into MySQL, and it's free on most common platforms (including Linux).

More especially if you are concerned about date, datetime datatypes, DocChow easily handles datatypes. If you are working with multiple Excel spreadsheets that you want to import into one MySQL table DocChow does the dirty work.

0

Step 1 Create Your CSV file

Step 2 log in to your mysql server

     mysql -uroot -pyourpassword 

Step 3 load your csv file

     load data local infile '//home/my-sys/my-excel.csv' into table my_tables fields terminated by ',' enclosed by '"' (Country, Amount,Qty);
0
0

Another useful tool, and as a MySQL front-end replacement, is Toad for MySQL. Sadly, no longer supported by Quest, but a brilliant IDE for MySQL, with IMPORT and EXPORT wizards, catering for most file types.

0

If you are using Toad for MySQL steps to import a file is as follows:

  1. create a table in MySQL with the same columns that of the file to be imported.
  2. now the table is created, goto > Tools > Import > Import Wizard
  3. now in the import wizard dialogue box, click Next.
  4. click Add File, browse and select the file to be imported.
  5. choose the correct dilimination.("," seperated for .csv file)
  6. click Next, check if the mapping is done properly.
  7. click Next, select the "A single existing table" radio button also select the table that to be mapped from the dropdown menu of Tables.
  8. Click next and finish the process.
0

If you don't like plugins, VBA and external tools, I have an excel file that using formulas only allows you to create INSERT/UPDATES. You only have to put the data on the cells:

enter image description here

As an extra, there's another tab in the file to CREATE TABLES:

enter image description here

The file can be found on the following link:

EXCEL FILE

0

I've had good results with the Tools / Import CSV File feature in HeidiSQL, with CSV files directly exported from Excel 2019 with "Save As..."

It uses LOAD DATA INFILE internally but with a GUI interface and also analyzes the CSV file before passing it to LOAD DATA INFILE so it can, for example, create the table using the first row as column names and guessing the column data type (<New table> option as shown in the picture)

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.