Monday, January 14, 2013

Exporting and importing data between MySQL and Microsoft excel

sumber : http://www.w3resource.com/mysql/exporting-and-importing-data-between-mysql-and-microsoft-excel.php

Description

In this page we will discuss how to import data from Microsoft Excel to MySQL and how to export data from MySQL to Microsoft Excel.
We will cover the topic in three parts -
1. Installing and configuring MySQL ODBC driver.
2. Import data from MySQL into Excel.
3. Export data from Excel into MySQL.

Installing and configuring MySQL ODBC driver

Download MySQL ODBC driver from MySQL dev.mysql.com/downloads/connector/odbc/5.1.html.
MySQL ODBC driver is required for connecting to MySQL from Microsoft Excel.
Install MySQL ODBC driver by executing the msi file you have downloaded.
To configure the MySQL ODBC driver (we have used Windows 7, 32bit), open Data Sources(ODBC) by clicking Start > Control Panel > System and Security > Administrative Tools > Data Sources(ODBC).
mysql odbc configuration
Click on Add button, scroll down to select MySQL ODBC 5.1 driver from the list and click finish.

mysql odbc driver configuration 2
This will ask to provide information necessary to connect to the MySQL server. You have to select the database by clicking the arrow next to the database text box.
mysql-odbc-driver-configuration3
Now click on the Test button and if everything goes well, we will see a message like bellow.
mysql-odbc-driver-configuration4
Continue to click on OK buttons (thrice) now to complete the configuration.

how to Import data from MySQL into Excel

In Excel (we have used Microsoft Office 2003), open Select Data Source by clicking Data > Import External Data > Import Data.
MySQL ODBC driver is required for connecting to MySQL from Microsoft Excel.
Install MySQL ODBC driver by executing the msi file you have downloaded.
To configure the MySQL ODBC driver (we have used Windows 7, 32bit), open Data Sources(ODBC) by clicking Start > Control Panel > System and Security > Administrative Tools > Data Sources(ODBC).
import-data-from-excel-to-mysql
Click on New Source button, select Other/advanced and click Next.
In the next window, select connection tab and provide all the necessary information as shown bellow.

import-data-from-excel-to-mysql3
Click on the Test Connection tab and if everything goes well, you will see a message like bellow.
import-data-from-excel-to-mysql4
Now click on the Test button and if everything goes well, we will see a message like bellow.
mysql-odbc-driver-configuration4
Select the MySQL table from where you want to import data and click Next.
import-data-from-excel-to-mysql5
Add the description and click Finish.
import-data-from-excel-to-mysql6
Select the data source in the next window and click Open.
import-data-from-excel-to-mysql7
The very next window will ask you whether you want to save the data in the existing worksheet or some where else. We opted for existing worksheet.
import-data-from-excel-to-mysql8
Click OK now. Data from book_mast table of bookinfo database is imported to the Excel now.
import-data-from-excel-to-mysql9

how to Export data from Excel into MySQL

Now, we will discuss how to Export data from Excel into MySQL.
And Import data from MySQL into Excel in the second part.

Export data from Excel into MySQL

We will create a copy of author table of bookinfo database and name it author_2. For that we will execute the following command -
CREATE TABLE author_2 LIKE author;
Result of the command will be like following -
Export-data-from-Excel-into-MySQL
Export the data from customer table to Excel and save it as author.csv.
Import the author.csv file into the author_2 table using LOAD DATA INFILE statement.
load data infile 'D:\\author.csv' into table author_2 fields terminated by ',' lines terminated by '\n';
This is what happens when you run the above command in the MySQL prompt.

Export-data-from-Excel-into-MySQL2