0

我正在使用 PHPExcel 更新 eshop 的商品价格和库存,将数据传递给 mysql 查询并更新数据库。我遇到了一些包含格式为 123,45 的数字的单元格(使用逗号作为小数分隔符)的问题。尽管所有单元格都包含相同的格式,但只有一些单元格被正确读取。请自己看看,让我知道我错过了什么。

最后,我的代码:

    $objPHPExcel = PHPExcel_IOFactory::load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

    $highestRow = $objWorksheet->getHighestRow();
    $highestColumn = $objWorksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;

    $rowsadded=0;
    $begin_row=2; // 1st line of data in excel file
    for ($row = $begin_row; $row <= $highestRow; ++ $row) {
        $val=array();
        for ($col=0; $col < $highestColumnIndex; $col++) {
            $cell = $objWorksheet->getCellByColumnAndRow($col, $row);
            $val[] = $cell->getCalculatedValue();
        }

        if (($val[0]<>'') && ($val[6]>0)) { //check that row contains data before inserting
            $rowsadded++;
            $sql = sprintf("update productsizes set price=%s, stock=%s where auxcode=%s",
                   GetSQLValueString($val[6], "float"),
                   GetSQLValueString($val[4], "int"),
                   GetSQLValueString($val[0], "text"));
            $result = mysql_query($sql) or die(mysql_error());
        } //end of checking that row contains data before inserting

    } echo '<h2>'.$rowsadded. ' rows updated successfully</h2>';
4

2 回答 2

0

我没有使用 PHPExcel,但你为什么不改变它$val[] = $cell->getCalculatedValue();来获取字符串值,然后,.& 替换变量作为整数并将其存储在另一个变量/数组中?我敢打赌这会奏效。

于 2012-06-21T15:16:49.790 回答
0
$rowsadded=0;
$begin_row=2; // 1st line of data in excel file
for ($row = $begin_row; $row <= $highestRow; ++ $row) {
    $val = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE,TRUE);
    var_dump($val);
    $val = $val[$row];

    if (($val['A']<>'') && ($val['G']>0)) { //check that row contains data before inserting
        $rowsadded++;
        // other code goes in here
    } //end of checking that row contains data before inserting

} echo '<h2>'.$rowsadded. ' rows updated successfully</h2>';

var_dump() 将在您加载时向您显示每行的值和数据类型。将此用于调试目的

于 2012-06-21T16:20:03.193 回答