如何在Laravel Excel上预格式化单元格到目前为止

时间:2015-04-24 15:55:09

标签: php excel laravel cell-formatting laravel-excel

我正在使用包maatwebsite.com/excel从我的应用中导出xls个文件。我想预先格式化单元格。有可能吗?

我已将数据格式化为日期,但我希望该单元格预先格式化为date(其默认单元格格式为general)。如何在PHPLaravel 4

中实现这一目标

4 个答案:

答案 0 :(得分:2)

你试过这个吗?

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

来自文档:http://www.maatwebsite.nl/laravel-excel/docs/export#format

此处提供了不同的日期格式:http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#formatting

答案 1 :(得分:1)

列格式

要告诉Excel它应该如何解释某些列,您可以使用 - > 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',
));

<强>价: http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#sheet-properties

答案 2 :(得分:0)

我使用了这种方法:

要读取数据:

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

要格式化:

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

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

我添加了此用法:

use PhpOffice\PhpSpreadsheet\Shared\Date;

答案 3 :(得分:0)

如果您需要动态解决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;
    }
    
}