【问题标题】:How Can I set dynamic number format with separator in laravel excel maatwebsite?如何在 laravel excel maatwebsite 中设置带分隔符的动态数字格式?
【发布时间】:2022-07-26 15:02:15
【问题描述】:

我有一个金额列,我想在数字没有小数时设置整数格式,当数字有小数时设置双精度格式。在这两种方式中,我都想为数字添加分隔符。 目前,我使用 bindValue 但 excel 单元格不知道金额列为数字格式,我应该选择它们并将它们转换为数字。

public function bindValue(Cell $cell, $value)
{
    if (is_int($value)) {
        $cell->setValueExplicit($value, '#,##0');

        return true;
    }else if (is_double($value)) {
        $cell->setValueExplicit($value, '#,##0.00');

        return true;
    }else{
        $cell->setValueExplicit($value,  DataType::TYPE_STRING);

        return true;

    }
}

我该如何解决?

【问题讨论】:

    标签: excel laravel number-formatting maatwebsite-excel laravel-excel


    【解决方案1】:

    使用WithColumnFormatting

    namespace App\Exports;
    
    use PhpOffice\PhpSpreadsheet\Shared\Date;
    use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
    use Maatwebsite\Excel\Concerns\WithColumnFormatting;
    use Maatwebsite\Excel\Concerns\WithMapping;
    
    class InvoicesExport implements WithColumnFormatting, WithMapping
    {
        public function map($invoice): array
        {
            return [];
        }
        
        public function columnFormats(): array
        {
            return [
                'B' => NumberFormat::FORMAT_NUMBER_00,
                'C' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
            ];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多