How to import Excel file into mysql database using

2020-03-31 02:02发布

问题:

I want to import from excel sheet format (.xls) to mysql database through phpmyadmin importing option. I understand that we need to convert the format to csv format first before we can import to the phpmyadmin. But unfortunately if I change to csv some special character or symbol will become question mark (?) or other different character/symbol. Please advise me on this as I am really new to phpmyadmin.

Thanks

回答1:

I have answered similary question here https://stackoverflow.com/a/16330428/1570901

If you are familiar with html and php, by using this simply library simplex excel library and script you can create your own excel import to mysql. IT may take few minutes to create but once your create you can use it for life time.

// CREATE A HTML FORM TO UPLOAD EXCEL SHEET

// THEN CREATE A PHP SCRIPT LIKE BELOW

require 'simplexlsx.class.php';

if (isset($_FILES['Filedata'])) {

$file = $_FILES['Filedata']['tmp_name']; // UPLOADED EXCEL FILE

$xlsx = new SimpleXLSX($file);

list($cols, $rows) = $xlsx->dimension();

foreach( $xlsx->rows() as $k => $r) { // LOOP THROUGH EXCEL WORKSHEET

$q = "INSERT INTO TABLENAME(COL1, COL2) VALUE(";
  $q .=  "'".mysql_escape_string($r[0])."', "; // EXCEL DATA
  $q .=  "'".mysql_escape_string($r[1])."', "; // EXCEL DATA
  $q .= ")";

  $sql = mysql_query($q);

    } // IF ENDS HERE
    } // FOR EACH LOOP
}