如果value为Empty,则PHPExcel_shared_date :: ExcelToPHP返回当前日期

时间:2014-09-05 07:52:43

标签: php phpexcel

我使用PHPExcel将数据从excel导入PHP。

我正在使用PHPExcel_shared_date::ExcelToPHP获取一个日期列如果我在单元格中加上有效日期,那么这是正确的,但问题是否我将其设为空白导入时,需要当前日期。

    for ($row = 2; $row <= $highestRow + 1; $row++) {
        $date_cell = $ci->excel->setActiveSheetIndex(0)->getCell('D' . $row)->getValue();
        //echo $date_cell;
        if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
            if (isset($detail_array) && !empty($detail_array))
                $data[] = $detail_array;
        }
        if (in_array($row, $merge_row)) {
            $i = 0;
            $detail_array = array();
            $detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
        } else {
            for ($col = 0; $col < count($field); $col++) {
                $val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
                if ($value_cell && $val) {
                        $detail_array['detail'][$i][$field[$col]] = $val;
                        $detail_array['detail'][$i]['date'] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($date_cell));
                }
            }
            $i++;
        }
    }

    print_r($data);

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

流程存在问题,您可以这样做:

    for ($row = 2; $row <= $highestRow + 1; $row++) {
        if ($row == $highestRow + 1 || in_array($row, $merge_row)) {
            if (isset($detail_array) && !empty($detail_array))
                $data[] = $detail_array;
        }
        if (in_array($row, $merge_row)) {
            $i = 0;
            $detail_array = array();
            $detail_array['account'] = $ci->excel->setActiveSheetIndex(0)->getCell("A" . $row)->getvalue();
        } else {
            $alpha = "A";
            for ($col = 0; $col < count($field); $col++) {
                $val = $workSheet->getCellByColumnAndRow($col, $row)->getValue();
                $detail_array['detail'][$i][$field[$col]] = "";
                if ($value_cell && $val && trim($val) != "") {
                    if (PHPExcel_Shared_Date::isDateTime($ci->excel->setActiveSheetIndex(0)->getCell($alpha . $row)))
                        $detail_array['detail'][$i][$field[$col]] = date('d-m-y', PHPExcel_Shared_Date::ExcelToPHP($val));
                    else
                        $detail_array['detail'][$i][$field[$col]] = $val;
                }
                $alpha++;
            }
            $i++;
        }
  }

  print_r($data);