3

I am using the package maatwebsite.com/excel for exporting xls files from my app. I want to pre-format the cell to date. Is it possible?

I have formatted the data to date, but I want that the cell would be pre-formatted with date (its default cell format is general). How can I achieve this in PHP or Laravel 4?

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
Jay Marz
  • 1,861
  • 10
  • 34
  • 54
  • It is unclear on what you are currently getting. Is it a date serial number like *42,118* or are you achieving a default *Short Date* (e.g. *04/24/2014*) but wish something more elaborate? –  Apr 25 '15 at 01:02

4 Answers4

1

Have you tried this yet?

$sheet->setColumnFormat(array(
    'A' => 'yyyy-mm-dd'
));

From the documentation: http://www.maatwebsite.nl/laravel-excel/docs/export#format

Different date formats are available here: http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#formatting

haakym
  • 12,050
  • 12
  • 70
  • 98
1

Column formatting

To tell Excel how it should interpret certain columns, you can use ->setColumnFormat($array).

Column formatting

To tell Excel how it should interpret certain columns, you can use ->setColumnFormat($array).


// Format column as percentage
$sheet->setColumnFormat(array(
    'C' => '0%'
));

// Format a range with e.g. leading zeros
$sheet->setColumnFormat(array(
    'A2:K2' => '0000'
));

// Set multiple column formats
$sheet->setColumnFormat(array(
    'B' => '0',
    'D' => '0.00',
    'F' => '@',
    'F' => 'yyyy-mm-dd',
));

Ref: http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#sheet-properties

matinict
  • 2,411
  • 2
  • 26
  • 35
0

I used this approach:

To read data:

Date::dateTimeToExcel($data->created_at),

To format:

$sheet->setColumnFormat(array(
  'P2:P'.$rows => 'dd/mm/yyyy',
));

Source: https://laravel-excel.maatwebsite.nl/3.1/exports/column-formatting.html#formatting-columns

I added this use:

use PhpOffice\PhpSpreadsheet\Shared\Date;
jpussacq
  • 573
  • 9
  • 29
0

If your need involves resolving datetime fields dynamically, I have written a method that is responsible for automatically detecting if the value is a datetime dynamically (regardless of whether or not you know if there will be a datetime in that column) or I have tried various data types and it works fine

   /**
 * @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;
    }
    
}