如何使用PHPExel在Excel文件中获取A列中所有行的数组?

时间:2015-08-17 20:08:53

标签: php class phpexcel

我有一个电话号码(A行)的Excel文件列表。 如何使用PHPExel将其作为数组获取?

我可以得到一个完整的例子,包括对'Class'目录中哪个文件的一个小解释我应该'include_once'?我怎么知道要包含哪个.php文件?如何扫描列表?

1 个答案:

答案 0 :(得分:2)

      <?php
/************************ YOUR DATABASE CONNECTION START HERE   ****************************/

define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", "root"); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME","database Name here"); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

$databasetable = ""; // your table name

/************************ YOUR DATABASE CONNECTION END HERE  ****************************/

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel-develop\Classes\PHPExcel\IOFactory.php';

    $targetfolder = "";
    $targetfolder = $targetfolder . basename( $_FILES['fileToUpload']['name']) ;

    if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $targetfolder))
    {
        echo "The file ". basename( $_FILES['fileToUpload']['name']). " is uploaded";
    }

    else 
    {
        echo "Problem uploading file";
        print $targetfolder . basename( $_FILES['fileToUpload']['name']) ;
    }

    $inputFileName = basename( $_FILES['fileToUpload']['name']);

    try 
    {
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
    } 
    catch(Exception $e) 
    {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

    $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
    $arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet

    for($i=2;$i<=$arrayCount;$i++)
    {
        $value1 = trim($allDataInSheet[$i]["A"]);
        $Value2 = trim($allDataInSheet[$i]["B"]);
        $value3 = trim($allDataInSheet[$i]["C"]);


        $query = "SELECT * FROM "; // your select query
        $sql = mysql_query($query);
        $recResult = mysql_fetch_array($sql);
        $exist = $recResult["$value1"];

        if($exist=="") 
        {
            $insertTable= mysql_query("//your insert query");

        $msg = 'Record has been added.</div>';
        } 
        else 
        {
            $msg = 'Record already exist. </div>';
        }
    }

?>