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
jarrodwhitley
  • 826
  • 10
  • 29
Fero
  • 12,969
  • 46
  • 116
  • 157
  • 1
    This free utility makes importing excel spreadsheets into mysql tables quick and easy http://panofish.net/convert-import-excel-spreadsheets-into-mysql-database/ – panofish Jun 13 '13 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 '13 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 '14 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 '14 at 05:02
  • excellent !!! how about upserts ;o) ?! – Yordan Georgiev May 18 '14 at 19:41
  • [You might take a look at this thread][1] on the MySQL forums. It explains how to do exactly what you want. [1]:http://forums.mysql.com/read.php?32,216343,216344#msg-216344 – Kredns Aug 21 '09 at 05:11
  • @Fr0zenFyr, I have completely rewritten my program and I would like to provide a free copy to you. I want your unbiased feedback, because I believe it is the solution the OP needs. This way others and the OP can get a true opinion and not just my own. – panofish Jul 18 '14 at 04:18
  • @panofish I'd be glad to do the test for you and offer my reviews. Thanks for finding me again and making the request. Is the link same as before to download updated program? – Fr0zenFyr Jul 18 '14 at 11:11
  • Yes... it simply redirects to the new version, since I made a new website for it at http://excel2mysql.net – panofish Jul 18 '14 at 13:25
  • Now if only @panofish's program would do it without littering the database with null entries it would actually be useful. As it stands, I now have to go through and manually clean up the database or drop it and look for a different solution. – Kenny Johnson Sep 26 '14 at 20:53
  • @KennyJohnson, Wish granted. http://www.excel2mysql.net/version220.html Contact me direct for more feedback. Thanks – panofish Sep 27 '14 at 00:58

15 Answers15

132

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)

d4nt
  • 15,475
  • 9
  • 42
  • 51
  • 33
    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. – Chris Schmitz Aug 02 '16 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 '16 at 16:24
  • @doxsi it seems fine to me, what sort of problem did you have with it? – d4nt Mar 21 '17 at 10:41
  • @ChrisSchmitz we are super-cautious with security and wrote a blog post recently about how we operate SQLizer http://blog.sqlizer.io/posts/privacy-at-sqlizer/ – a_good_swan Mar 28 '17 at 15:52
  • It works, pay attention at the Excel format, xlt isn't accepted meanwhile xls is accepted. – Emanuel Pirovano Apr 05 '18 at 10:19
  • It was updated to $25/month, might as well build your own tool to do this and use that one forever. – qwertzman Jun 06 '18 at 14:44
  • This is not working with a .xslx file and the only error message I get is: This isn't working out. I'm sorry, we weren't able to convert your file. No indication of anything I may have done wrong, – Stuart Jul 23 '19 at 12:44
  • @TiagoMartinsPeres ok, I've added a disclaimer. It's a freemium tool that I run part time and I was only trying to be helpful. – d4nt Oct 01 '19 at 08:55
  • Understandable. I've done a quick search and most of the posts including sqlizer.io were from you and / or other team members and none disclose the affiliation. – Tiago Martins Peres Oct 01 '19 at 08:56
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.

Community
  • 1
  • 1
thdoan
  • 18,421
  • 1
  • 62
  • 57
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.

ashatte
  • 5,442
  • 8
  • 39
  • 50
ndp
  • 21,546
  • 5
  • 36
  • 52
  • 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. – Tadas S Apr 12 '14 at 15:14
  • 1
    Note that this procedure requires that you first create the table, with the appropriate fields. – LarsH Mar 24 '16 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 '16 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 Remember Monica Oct 24 '17 at 14:06
  • 1
    *You meant "The hardest way" not the easiest – Revious Jul 08 '19 at 16:19
  • you also have to consider permissions and server configurations. anywhere with security is probably gonna have this setting off – Zero Jun 05 '21 at 00:47
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

panofish
  • 7,578
  • 13
  • 55
  • 96
  • 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 '14 at 07: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 '14 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 '14 at 02:34
  • 2
    Excel2MySQL tool should have the possibility to only create the database script :( – joseantgv Jul 29 '15 at 14:00
  • PHPMyAdmin was the easiest and quickest one. – Amir Hajiha Sep 07 '18 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.

user1441213
  • 169
  • 1
  • 6
  • 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 '21 at 15:53
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.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
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

Sadeq Shajary
  • 427
  • 6
  • 17
1

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

Daniel D
  • 79
  • 1
  • 4
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

Raúl Moreno
  • 311
  • 3
  • 14
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.

Seanj1000
  • 11
  • 1
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);
Syed Shibli
  • 992
  • 1
  • 12
  • 15
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.

Fandango68
  • 4,461
  • 4
  • 39
  • 74
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.
tushar_lokare
  • 461
  • 1
  • 8
  • 22
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

Kaiser
  • 1,957
  • 1
  • 19
  • 28
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

golimar
  • 2,419
  • 1
  • 22
  • 33