3

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

我已经格式化了迄今为止的数据,但我希望单元格将被预先格式化date(其默认单元格格式是general)。我怎样才能在PHPor中实现这一点Laravel 4

4

4 回答 4

1

你试过这个吗?

$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

于 2015-04-24T20:02:13.017 回答
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

于 2017-02-13T05:50:25.890 回答
0

如果您需要动态解析日期时间字段,我编写了一个方法,负责自动检测该值是否是动态日期时间(无论您是否知道该列中是否会有日期时间),或者我尝试了各种数据类型,它工作正常

   /**
 * @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;
    }
    
}
于 2020-11-21T23:32:34.803 回答
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;
于 2019-01-31T14:16:11.487 回答