使用phpexcel将myscel数组excel到mysql数据库

时间:2016-06-22 08:08:13

标签: php mysql excel phpexcel

我正在使用phpexcel将数据从xlsx输入到mysql数据库。 我的excel文件具有使用公式计算的值,例如:

  

= SUM(J21:J24)

我的phpexcel脚本运行正常,但使用excel文件中的公式的数据会在上传后显示' 0'

我的PhP脚本如下:

<?php  
 $connect = mysqli_connect("localhost", "root", "", "unhrd_fund_balance");  
 include ("PHPExcel/IOFactory.php");  
 $html="<table border='1'>";  
 $objPHPExcel = PHPExcel_IOFactory::load('sample2.xlsx');  
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)   
 {  
      $highestRow = $worksheet->getHighestRow();  
      for ($row=2; $row<=$highestRow; $row++)  
      {  
           $html.="<tr>";        

            $ini = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(0, $row)->getValue());
            $progkey = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(1, $row)->getValue());
            $grantkey = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(2, $row)->getValue());
            $tod = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(3, $row)->getValue());
            $tdd = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(4, $row)->getValue());
            $fund = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(5, $row)->getValue());
            $orderkey = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(6, $row)->getValue());
            $budgetalloc = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(7, $row)->getValue());
            $precommit = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(8, $row)->getValue());
            $commit = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(9, $row)->getValue());
            $actuals = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(10, $row)->getValue());
        $totalcommit = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(11, $row)->getValue()); 
            $availablebudget = mysqli_real_escape_string($connect,$worksheet->getCellByColumnAndRow(12, $row)->getValue());

            $query = "insert into fund_balances(INITIATIVE, FUNDED_PROG_KEY, GRANT_KEY, TOD, TDD, FUND, ORDER_KEY, BUDGET_ALLOC, PRE_COMMIT, COMMIT, ACTUALS, TOTAL_COMMIT,AVAILABLE_BUDGET) values('".$ini."','".$progkey."','".$grantkey."','".$tod."','".$tdd."','".$fund."','".$orderkey."','".$budgetalloc."','".$precommit."','".$commit."','".$actuals."','".$totalcommit."','".$availablebudget."')";

           mysqli_query($connect, $query);

           $html.= '<td>'.$ini.'</td>';  
           $html .= '<td>'.$progkey.'</td>';  
           $html .= '<td>'.$grantkey.'</td>';  
           $html .= '<td>'.$tod.'</td>';  
           $html .= '<td>'.$tdd.'</td>';  
           $html .= '<td>'.$fund.'</td>';  
           $html .= '<td>'.$orderkey.'</td>';  
           $html .= '<td>'.$budgetalloc.'</td>';  
           $html .= '<td>'.$precommit.'</td>';
           $html .= '<td>'.$commit.'</td>';  
           $html .= '<td>'.$actuals.'</td>';  
           $html .= '<td>'.$totalcommit.'</td>';  
           $html .= '<td>'.$availablebudget.'</td>';  
           $html .= "</tr>";  
      }  
 }  
 $html .= '</table>';  
 echo $html;  
 echo '<br />Data Inserted';  
 ?>

在上面的代码中,&#39; $ actuals&#39;使用excel公式,在数据库中为空。

1 个答案:

答案 0 :(得分:0)

单元格的getValue()方法将返回公式本身;如果您想获得该公式的评估结果,请使用getCalculatedValue()方法代替......,如PHPExcel Documentation

中所述