Php 如何在Laravel Excel上预先设置单元格的格式

Php 如何在Laravel Excel上预先设置单元格的格式,php,excel,laravel,cell-formatting,laravel-excel,Php,Excel,Laravel,Cell Formatting,Laravel Excel,我正在使用该软件包从我的应用程序导出xls文件。我想将单元格的格式预先设置为最新格式。可能吗 我已将数据格式化为最新格式,但我希望单元格将预先格式化为date(其默认单元格格式为general)。我如何在PHP或laravel4中实现这一点?您尝试过吗 $sheet->setColumnFormat(array( 'A' => 'yyyy-mm-dd' )); 从文件中: 此处提供了不同的日期格式:列格式 要告诉Excel它应该如何解释某些列,可以使用->setColumn

我正在使用该软件包从我的应用程序导出
xls
文件。我想将单元格的格式预先设置为最新格式。可能吗


我已将数据格式化为最新格式,但我希望单元格将预先格式化为
date
(其默认单元格格式为
general
)。我如何在
PHP
laravel4
中实现这一点?

您尝试过吗

$sheet->setColumnFormat(array(
    'A' => 'yyyy-mm-dd'
));
从文件中:

此处提供了不同的日期格式:

列格式

要告诉Excel它应该如何解释某些列,可以使用->setColumnFormat($array)

列格式
要告诉Excel它应该如何解释某些列,可以使用->setColumnFormat($array)。
//将列的格式设置为百分比
$sheet->setColumnFormat(数组)(
“C”=>“0%”
));
//使用前导零等格式设置范围
$sheet->setColumnFormat(数组)(
‘A2:K2’=>‘0000’
));
//设置多个列格式
$sheet->setColumnFormat(数组)(
“B”=>“0”,
'D'=>'0.00',
“F'=>'@',
'F'=>'yyyy mm dd',
));
Ref: 我使用了这种方法:

要读取数据:

Date::dateTimeToExcel($data->created_at),
要格式化:

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

我添加了以下用法:

use PhpOffice\PhpSpreadsheet\Shared\Date;

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

目前还不清楚你目前得到了什么。是42118之类的日期序列号,还是您实现了默认的短日期(如2014年4月24日),但希望更详细一些?Ariel,您能与我们分享您的解决方案吗?谢谢~:)我仍然停留在这里,仍然没有格式,@ArielMagbanua你能与我们分享吗?@AnthonyKal尝试在StackOverflow上创建你自己的问题或在这里创建问题