簡體   English   中英

如何導入Excel數據文件到MySQL數據庫?

[英]How to import excel data file to mysql database?

我使用了以下代碼,其中我必須選擇一個excel文件並將該文件數據上傳到數據庫中,但顯示錯誤:致命錯誤:調用E:\\ xampp \\ htdocs \\中未定義的函數uploadFile()第9行的php \\ exceltodb1.php幫助解決該問題。

<?php
require_once 'phpExcelReader/Excel/reader.php';
mysql_connect('localhost','root','');
mysql_select_db('world');
if(isset($_POST['submit']))
{
if($_FILES['excelFile']['name']!="")
    {
        $fileName=uploadFile($_FILES['excelFile'],array(".xls",".xlsx"),"excel_file");
        $data = new Spreadsheet_Excel_Reader();
        $data->read('excel_file/'.$fileName);
        for($i=1;$i<=$data->sheets[0]['numRows'];$i++)
        {
            $firstname=$data->sheets[0]['cells'][$i][1];
            $lastname=$data->sheets[0]['cells'][$i][2];
            $mobile=$data->sheets[0]['cells'][$i][3];
            $city=$data->sheets[0]['cells'][$i][4];
            $query="INSERT INTO city(city,id,stateid,countryid)
                        VALUES('".$firstname."','".$lastname."','".$mobile."','".$city."')";
            mysql_query($query);
        }
    }
}   
    ?>
    <html>
    <head></head>
    <body>
    <form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="excelFile" value="" >
    <input type="submit" name="submit" value="submit" >
    </form>
    </body>
</html>

為此,請使用PhpExcel庫,但在此上傳之前,您需要將文件歸檔到文件夾,並要求“ Classes / PHPExcel.php”; require_once'Classes / PHPExcel / IOFactory.php';

           $path =  "upload_file/" .  $filename; 

            $objPHPExcel = PHPExcel_IOFactory::load($path);
            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                $worksheetTitle     = $worksheet->getTitle();
                $highestRow         = $worksheet->getHighestRow(); // e.g. 10
                $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
                $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
                $nrColumns = ord($highestColumn) - 64;

                $test_array = array();

                for ($row = 1; $row <= $highestRow; ++ $row) {

                $a=$worksheet->getCellByColumnAndRow(1, $row);
                $firstname = $cell->getValue();
                $b=$worksheet->getCellByColumnAndRow(2, $row);
                $lastname = $b->getValue();
                $c=$worksheet->getCellByColumnAndRow(3, $row);
                $mobile = $c->getValue();
                $d=$worksheet->getCellByColumnAndRow(4, $row);
                $city = $d->getValue();
                   $query="INSERT INTO city(city,id,stateid,countryid)
                    VALUES('".$firstname."','".$lastname."','".$mobile."','".$city."')";
                    mysql_query($query);
                }   
            }

代碼未經測試,但會給您概述

您可以在這里參考我的教程。 它具有相關文件供下載並完整說明。 希望對您有幫助。

$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo "The worksheet ".$worksheetTitle." has ";
    echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
    echo ' and ' . $highestRow . ' row.';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM