[英]excel formula to mysql database using phpexcel
I am using phpexcel to enter data from xlsx to mysql database. 我正在使用phpexcel将数据从xlsx输入到mysql数据库。 My excel file have values calculated using formula, for eg:
我的Excel文件具有使用公式计算的值,例如:
=SUM(J21:J24)
= SUM(J21:J24)
My phpexcel script is running fine, however the data using a formula in excel file shows '0'after the upload. 我的phpexcel脚本运行良好,但是在excel文件中使用公式的数据在上传后显示为“ 0”。
My PhP script is as follows: 我的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';
?>
In the above code, the '$actuals' uses the excel formula and is null in database. 在上面的代码中,“ $ actuals”使用excel公式,并且在数据库中为null。
The cell's getValue()
method will return the formula itself; 单元格的
getValue()
方法将返回公式本身。 if you want to get the evaluated result of that formula, then use the getCalculatedValue()
method instead.... as explained in the PHPExcel Documentation 如果要获取该公式的求值结果,请改用
getCalculatedValue()
方法...。如PHPExcel文档中所述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.