PHPExcel验证单元格是否具有empy值

时间:2017-07-05 09:16:37

标签: php mysql pdo phpexcel

你好我导入了一个xls文件,上面有多行,只有当价格列的值不同于其他任何显示错误消息时,我才想将它们插入到数据库中。它工作一半它只插入那些具有价格价值的行,而是返回html错误msg它返回sql msg,这意味着它进入了else分支。 这是我的代码

$exceldata = array();

$uploadFilePath = 'uploads/'.basename($_FILES['doc']['name']);
        move_uploaded_file($_FILES['doc']['tmp_name'], $uploadFilePath);

        $inputfilename = 'uploads/'.$_FILES['doc']['name'].'';
//  Read your Excel workbook
try
{
    $inputfiletype = PHPExcel_IOFactory::identify($inputfilename);
    $objReader = PHPExcel_IOFactory::createReader($inputfiletype);
    $objPHPExcel = $objReader->load($inputfilename);
}
catch(Exception $e)
{
    die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

$header=$_POST['membership'];

if($header==1)
{
//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++)
{ 

    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
    if ($rowData[0][9]=='')
    {
        echo 'No price for the product '.$rowData[0][1].'';
    }
    else 
    {
    $a = array('8', '1', '2', '3', '4', '5','6');
    $b = array($rowData[0][0], $rowData[0][2], $rowData[0][3],$rowData[0][5],$rowData[0][6],$rowData[0][8],$rowData[0][8]);
    $c = array_combine($a, $b);

    $slug = str_replace(" ", "-",$rowData[0][1]);
$slug = str_replace('"', "",$slug);
$slug = str_replace('/', "-",$slug);
    $stmt=$dbh->prepare("INSERT INTO tbl_products (name,slug,description,price)
                    VALUES (:name,:slug,:desc,:pret)");

$stmt->bindParam(":name",$rowData[0][1]);
$stmt->bindParam(":slug",$slug);
$stmt->bindParam(":desc",$rowData[0][10]);
$stmt->bindParam(":pret",$rowData[0][9]);

$stmt->execute();
$id_product=$dbh->lastInsertId();

$stmt=$dbh->prepare("INSERT INTO tbl_products_images_gallery (id_product,name,image,sort_order)
                    VALUES (:id,:name,:image,100)");

$stmt->bindParam(":id",$id_product);
$stmt->bindParam(":name",$rowData[0][4]);
$stmt->bindParam(":image",$slug);

$stmt->execute();

$stmt=$dbh->prepare("SELECT id_category from tbl_catalog_categories where name=:name");
$stmt->bindParam(":name",$rowData[0][2]);
$stmt->execute();
if($row=$stmt->fetch())
{
    $id_cat=$row['id_category'];
}   

$stmt=$dbh->prepare("INSERT INTO tbl_products_to_categories (id_category,id_product)
                    VALUES (:id_cat,:id_prod)");

$stmt->bindParam(":id_cat",$id_cat);
$stmt->bindParam(":id_prod",$id_product);

$stmt->execute();

foreach($c as $key => $value)
    {

        $stmt=$dbh->prepare("INSERT INTO tbl_products_attributes_values (id_product,id_attribute,attribute_value)
                    VALUES (:id_product,:id_attribute,:value)");
$stmt->bindParam(":id_product",$id_product);
$stmt->bindParam(":id_attribute",$key);
$stmt->bindParam(":value",$value);
$stmt->execute();
    }


}
}
}

1 个答案:

答案 0 :(得分:0)

或许

if (empty($rowData[0][9]))

或测试null和空字符串 - nulls是完全有效的PHPExcel响应...

特别是

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

使用它的第二个参数是告诉PHPExcel如果单元格在电子表格中不存在则返回空值

虽然您可以将rangeToArray()调用更改为

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, '', TRUE, FALSE);

如果单元格不存在则返回空字符串而不是null