Search code examples
mysqlmysqlimport

How can i import chinese data from excel to mysql?


I'm a excel file where i've Chinese content and i want to import them into my tables.

My table is set utf-8 collation and my excel sheet has three worksheets.

When i tried to convert the worksheet into csv file and use them for import but when i opened the csv file it doesn't have the chinese characters properly..

And i tried to convert them into txt file and then import and nothing seems to be working..

How can i import chinese content into my mysql table?

Update:

I tried your answers but didn't work on my way.. So i asked for a new file from the client from his native os. My client sent me the .csv file with chinese data, when i opened it in excel, it has some odd characters but when i opened it using dreamweaver it showed the chinese characters.

When i tried to import data using load data it has the same weird characters inside the db .. Then i uploaded the file to the server and tried into insert into db using php script , it also doesn't work..

The script i used

<?php
  header('Content-type: text/html; charset=utf-8');
  include("includes/config.php");
  db_connect();
  mysql_query("SET NAMES utf8");
  $file = fopen("../file.csv", "r");
  $i = 0;
  while (!feof($file)) {
      $arr = fgetcsv($file);
      $qry = "insert into tbl_wine_tasting_notes
    (`color` , `name` , `producer` , `vintage` , `size` , `country` , `region` ,
    `sub_region` , `comments` , `alcohol` , `points` , `varietals` ,
    `website`,`label`)
    values 
    ('" . implode("','", $arr) . "')";

      mysql_query("SET NAMES utf8");
      mysql_query($qry);
  }
  fclose($file);
?> 

The qry statement was correct when i echoed that but it didn't even inserted a single row..

What's wrong with this? I need them to upload them ..


Solution

  • Try using iconv to convert your .csv file from the major Chinese encodings below, transforming to UTF-8. The first '-f' from encoding that doesn't result in an error will be the correct encoding of your .csv file. From the command line on a system with iconv, try this:

    iconv -f [Chinese encoding] -t UTF-8 [filename].csv > [filename]_utf8.csv
    

    For the -f flag value, try:

    1. CP936
    2. GB18030
    3. BIG-5

    For example, I would guess that your .csv file was probably created on a Chinese Windows environment, so this could work:

    iconv -f CP936 -t UTF-8 [filename].csv > [filename]_utf8.csv
    

    You would then be able to open up [filename]_utf8.csv in a UTF-8 friendly text editor and see the Chinese displayed properly. It is this UTF-8 encoded [filename]_utf8.csv file that you would use to load the data into your database.