Laravel excel导入,excel单元格中的日期列作为浮动值返回。如何解决这个问题?

时间:2016-05-05 06:56:26

标签: laravel

通过使用Maatwebsite / Laravel-Excel导入excel表,这里我面临excel表的发布日期时间列返回浮点值。怎么解决这个? 示例:考虑单元格值" 08-04-2016 13:08:29"并返回" 42104.487060185"进口时。

4 个答案:

答案 0 :(得分:4)

这仅在使用块时发生。此问题可以通过以下方法解决:

$UNIX_DATE = ($row->DOB - 25569) * 86400;
$date_column = gmdate("d-m-Y H:i:s", $UNIX_DATE);

答案 1 :(得分:2)

已知错误,有关详细信息,请参阅https://github.com/Maatwebsite/Laravel-Excel/issues/404

但基本上,当使用chunk()读取单元格时,它无法将Excel的日期时间格式从float转换为Carbon日期对象。

目前没有修复,您可以通过在调用load之前调用config来解决此问题:

config(['excel.import.dates.columns' => [
        'deleted_at,
        'updated_at'
    ]]);

Excel::filter('chunk')->load($file)->chunk(100 function($rows) { ... });

如果您没有使用块过滤器,请参阅http://www.maatwebsite.nl/laravel-excel/docs/import#dates有关如何在单元格上设置格式(setDateColumns()),但除非您更改默认值,否则应自动转换。

答案 2 :(得分:0)

将导入文件的格式更改为.csv并将日期列的格式更改为所需的日期格式(dd-mm-yyyy)

答案 3 :(得分:0)

该“浮点数”是excel时间戳,因此它在内部存储了日期和时间数据。

例如:

123213.0: it's just a date
213233.1233: is a date and time
0.1233: it's one hour

要解决此问题,您必须将该浮点数转换为日期。

如果您需要动态解决datetime字段,我编写了一种方法,负责自动检测该值是否为动态datetime(无论您是否知道该列中是否有datetime),或者尝试了各种数据类型,效果很好

   /**
 * @param Cell $cell
 * @param $value
 * 
 * @return boolean;
 */
public function bindValue(Cell $cell, $value)
{
    $formatedCellValue = $this->formatDateTimeCell($value, $datetime_output_format = "d-m-Y H:i:s", $date_output_format = "d-m-Y", $time_output_format = "H:i:s" );
    if($formatedCellValue != false){
        $cell->setValueExplicit($formatedCellValue, DataType::TYPE_STRING);
        return true;
    }

    // else return default behavior
    return parent::bindValue($cell, $value);
}


/**
 * 
 * Convert excel-timestamp to Php-timestamp and again to excel-timestamp to compare both compare
 * By Leonardo J. Jauregui ( @Nanod10 | siskit dot com )
 * 
 * @param $value (cell value)
 * @param String $datetime_output_format
 * @param String $date_output_format
 * @param String $time_output_format
 * 
 * @return $formatedCellValue
 */
private function formatDateTimeCell( $value, $datetime_output_format = "Y-m-d H:i:s", $date_output_format = "Y-m-d", $time_output_format = "H:i:s" )
{

    // is only time flag
    $is_only_time = false;
    
    // Divide Excel-timestamp to know if is Only Date, Only Time or both of them
    $excel_datetime_exploded = explode(".", $value);

    // if has dot, maybe date has time or is only time
    if(strstr($value,".")){
        // Excel-timestamp to Php-DateTimeObject
        $dateTimeObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value);
        // if Excel-timestamp > 0 then has Date and Time 
        if(intval($excel_datetime_exploded[0]) > 0){
            // Date and Time
            $output_format = $datetime_output_format;
            $is_only_time = false;
        }else{
            // Only time
            $output_format = $time_output_format;
            $is_only_time = true;
        }
    }else{
        // Only Date
        // Excel-timestamp to Php-DateTimeObject
        $dateTimeObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value);
        $output_format = $date_output_format;
        $is_only_time = false;
    }
        
    // Php-DateTimeObject to Php-timestamp
    $phpTimestamp = $dateTimeObject->getTimestamp();

    // Php-timestamp to Excel-timestamp
    $excelTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $phpTimestamp );
        
    // if is only Time
    if($is_only_time){
        // 01-01-1970 = 25569
        // Substract to match PhpToExcel conversion
        $excelTimestamp = $excelTimestamp - 25569;
    }

    /* 
    // uncoment to debug manualy and see if working
    $debug_arr = [
            "value"=>$value,
            "value_float"=>floatval($value),
            "dateTimeObject"=>$dateTimeObject,
            "phpTimestamp"=>$phpTimestamp,
            "excelTimestamp"=>$excelTimestamp,
            "default_date_format"=>$dateTimeObject->format('Y-m-d H:i:s'),
            "custom_date_format"=>$dateTimeObject->format($output_format)
        ];
        
    if($cell->getColumn()=="Q"){
        if($cell->getRow()=="2"){
            if(floatval($value)===$excelTimestamp){
                dd($debug_arr);
            }
        }
    }

    */
    
    // if the values match
    if( floatval($value) === $excelTimestamp ){
        // is a fucking date! ;)
        $formatedCellValue = $dateTimeObject->format($output_format);
        return $formatedCellValue;
    }else{
        // return normal value
        return false;
    }
    
}