简体   繁体   中英

excel formula to mysql database using phpexcel

I am using phpexcel to enter data from xlsx to mysql database. My excel file have values calculated using formula, for eg:

=SUM(J21:J24)

My phpexcel script is running fine, however the data using a formula in excel file shows '0'after the upload.

My PhP script is as follows:

<?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';  
 ?>

In the above code, the '$actuals' uses the excel formula and is null in database.

The cell's getValue() method will return the formula itself; if you want to get the evaluated result of that formula, then use the getCalculatedValue() method instead.... as explained in the PHPExcel Documentation

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