Php - 阅读Excel和Strore到Db

时间:2015-11-25 07:38:09

标签: php arrays excel phpexcel

<?php


        $host  ="******.com";
        $uname = "*****";
        $pwd = "*******";
        $db = "*****_db";


    $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");

        $id = $_REQUEST['id'];
        $int_id = intval($id);
        $value = 0;

           if (isset($_REQUEST['vote1']))
            {
               $vote1 = $_REQUEST['vote1'];
               $value = 1;
               $vote = intval($vote1);
             }

             if (isset($_REQUEST['vote2']))
            {

                $vote2 = $_REQUEST['vote2'];
                $vote = intval($vote2);
                $value = 2;

            }

            //Get Detail



mysql_query("SET NAMES 'utf8'");
mysql_query ("SET CHARACTER SET utf8");
mysql_query ("SET COLLATION_CONNECTION = 'utf8_general_ci'");


// get all products from products table
$res_detail = mysql_query("SELECT *FROM v_list WHERE id='$int_id'") or die(mysql_error());


if (mysql_num_rows($res_detail) > 0) {

 while ($row = mysql_fetch_assoc($res_detail)) {

    if($value ==1 )
     {
      $fark = $vote1 - $row["vs1_vote"];

        if($fark == 1)
       {
        $sum_vote =  $vote1 + $row["vs2_vote"] ;

        $v1_percent = round( ($vote1 *100 /  $sum_vote), 2);

         $v2_percent = 100 - $v1_percent;

        }
          else
         {
          echo "An error occoured";

           }

      }//value1

  if($value ==2 )
    {
      $fark = $vote2 - $row["vs2_vote"];

      if($fark == 1)
       {
        $sum_vote =  $row["vs1_vote"] + $vote2 ;

        $v2_percent = round(($vote2 *100 /  $sum_vote),2);
        $v1_percent = 100 - $v2_percent;


        }
          else
         {
          echo "An error occoured";

           }
    }//value2



 }//while

}//if

$date = date('Y-m-d H:i:s');


    $flag['code']=0;

    if($value ==1 )
    {

      if($r=mysql_query("UPDATE v_list SET vs1_vote='$vote',vs1_percent='$v1_percent',vs2_percent='$v2_percent',allvotes='$sum_vote' WHERE id='$int_id'",$con))
      {
        $flag['code']=1;
        echo"hi";
      }

    }

    if($value ==2 )
    {  

      if($r=mysql_query("UPDATE v_list SET vs2_vote='$vote',vs1_percent='$v1_percent',vs2_percent='$v2_percent',allvotes='$sum_vote' WHERE id='$int_id'",$con))
      {
        $flag['code']=1;
        echo"hi";
      }

    }


    print(json_encode($flag));


    mysql_close($con);

?>

我使用上面的代码使用 PHPExcel 读取excel文件。我设法读取excel值并打印出来。现在的问题是我需要将值存储在数据库中。在这种情况下,如何从$ rowData获取值?
以下是示例输出:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$inputFileName ="../brief/phaseupload/" . $filename;
//  Read your Excel workbook
        try {
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
//  Loop through each row of the worksheet in turn
for ($row = 3; $row <= $highestRow; $row++){
//  Read a row of data into an array
 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
print_r($rowData);

1 个答案:

答案 0 :(得分:0)

看到您已经拥有阵列上的行数,您可以在循环内继续执行此操作。

$value1 = $rowData[$row]["A"];
$value2 = $rowData[$row]["B"];
   //and so on...

确保在插入数据库时​​清理字符串。