简体   繁体   中英

Import Excel into SQL database using php

I looked everywhere for this code but most of them are dedicated or just have errors and none work.

I want to import an Excel file into my sql database using php codes. Till now I have tried over 5 codes and plugins (in github) but last code has best result but still doesn't work.

$file = "../uploads/".$newfilename;
//echo $file;
$file_open = fopen($file,"r");
while(($csv = fgetcsv($file_open, 1000, ";")) !== false)
{
    $data = array_map("utf8_encode", $csv);
    print_r($data);
    $name = $csv[0];
    $pro_name = $csv[0];
    $brand = $csv[0];
    $tell = $csv[0];
    try{
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $db->prepare("INSERT INTO companies 
            (company_name, pro_name, brand, tell)
            VALUES(:a1, :a2, :a3, :a4)");
        $stmt->bindparam(':a1',$name);
        $stmt->bindparam(':a2',$pro_name);
        $stmt->bindparam(':a3',$brand);
        $stmt->bindparam(':a4',$tell);
        $stmt->execute();
        echo 'done';
    }catch(PDOException $e){
        //echo "error";
    }
}

The result from this code is this:

Array
(
    [0] => PK!ª÷X¤z[Content_Types].xml ¢( ÌTÉjÃ0½úF×+I¡'.Ç6ôklØÐLÒäï
    [1] => vJÈi ½ØØÒ¼eyѪ*£%4Î&¢wE6uÚØ<ӷΣժt±£áíÍ`ºöW[LDAä¤Ä´Jaì<X>É\¨ñgÈ¥Wé\å ûÝîL%°Ô¡C/©EIÑëoÌÑóæ^Må}iRE,T.­> é¸,3)h.*ÑPªÊØÃaDl<Ê Äv¤[W1W6°0ïØú   úä´«mÝ#
ÑXzW{«R~¹097Ï´mMÓ¢¸RÆîtáo.£l^½+©ý5À-uôÿû?ÒA¼s çïGÒÀ\Òº¼²Û
è%æBÐâmί.à'ö91ãà<rhß]dÔÕÏ@ÈÀ>4-ß#°=áA0B±ônÙdúðÿÿPK!µU0#õL_rels/.rels ¢( 
)

I have removed this line also : $data = array_map("utf8_encode", $csv); but it doesn't make any difference.

(Notice: I have to use utf8 unidcode because I have Persian words that need to be written in database!)

any solution?

  1. You use fgetcsv so you assume the given excel file is formatted as CSV.
  2. The "result" (which actually comes from your print_r($data); statement, so it's not really a result in any way), shows that the file is a zipped XML, likely to be the .xlsx format.

From just 2 points above, your current "looking for existing solution written by others" approach(*) may need to be improved. Get a proper Excel library (eg PhpSpreadsheet ), read and understand its documentation to be able to use it for your specific needs. Most important of all, try to write it your own, don't copy paste code you don't understand from wild wild web.

(*) I'm talking about these two sentences:

I looked everywhere for this code but most of them are dedicated or just have errors and none work.

Till now I have tried over 5 codes and plugins (in github) but last code has best result but still doesn't work.

Finally I found a website that has a full documents and description. In simple word the solution is to convert excel (.xlsx) file into XML (.xml) file and then import it with a simple code. https://www.ibm.com/developerworks/library/os-phpexcel/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM