【问题标题】:excel formula to mysql database using phpexcel使用phpexcel将excel公式转换为mysql数据库
【发布时间】:2016-06-22 08:08:13
【问题描述】:

我正在使用 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';  
 ?>

在上述代码中,'$actuals'使用excel公式,在数据库中为空。

【问题讨论】:

  • 您正在使用这些值创建一个 HTML 表。 '&lt;td&gt;'.$actuals.'&lt;/td&gt;' 是否也为 $actuals 显示 0?或者是其他东西?例如什么?还有,你的MySQL表中的字段ACTUALS是什么类型的字段数据?
  • 表格里面有一个公式,不是零。
  • ACTUALS 是 Excel 表格中用于计算值的公式
  • 这不是问题所在。问题是,您的MySQL 表 中的字段ACTUALS 是什么类型的字段数据?我怀疑它是数字。所以试试$actuals = mysqli_real_escape_string($connect,$worksheet-&gt;getCellByColumnAndRow(10, $row)-&gt;-&gt;getCalculatedValue());。这将得到计算值而不是公式。

标签: php mysql excel phpexcel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多