You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Excel导出浮点数末尾0丢失问题求助

解决Laravel Excel导出时保留指定小数位的问题

场景分析

如果数据库中存储的是数值类型(如float/double),1.10和1.1本质是同一个值,导出时自然会丢失末尾0;如果是decimal类型(如decimal(10,2)),则会强制保留两位小数,导致所有数值都补0。要实现"1.10显示为1.10,1.1显示为1.1"的效果,需要针对性处理:


方案1:数据库字段改为字符串存储(推荐,若业务允许)

直接将存储这类数值的字段设为varchar或string类型,存储"1.10"、"1.1"这样的字符串,导出时设置对应列为文本格式即可避免Excel自动处理:

use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class YourExport implements WithColumnFormatting
{
    public function columnFormats(): array
    {
        // 假设目标列是B列,设置为文本格式
        return [
            'B' => NumberFormat::FORMAT_TEXT,
        ];
    }

    public function collection()
    {
        // 直接取出数据库中的字符串数据即可
        return YourModel::all();
    }
}

方案2:动态判断小数位数设置格式(适用于数值类型字段)

如果无法修改数据库字段类型,可在导出时根据数值的实际小数位数动态设置单元格格式:

use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class YourExport implements WithStyles
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function styles(Worksheet $sheet)
    {
        $rowCount = $this->data->count() + 1; // 表头占1行
        // 遍历目标列的每一行(假设是B列)
        for ($row = 2; $row <= $rowCount; $row++) {
            $cellValue = $sheet->getCell("B$row")->getValue();
            // 判断小数位数
            $decimalPlaces = isset(explode('.', $cellValue)[1]) ? strlen(explode('.', $cellValue)[1]) : 0;
            
            $formatCode = match($decimalPlaces) {
                2 => NumberFormat::FORMAT_NUMBER_00,
                1 => NumberFormat::FORMAT_NUMBER_0,
                default => NumberFormat::FORMAT_NUMBER,
            };
            
            $sheet->getStyle("B$row")->getNumberFormat()->setFormatCode($formatCode);
        }
    }

    public function collection()
    {
        return $this->data;
    }
}

方案3:转换为带指定小数位的字符串后导出

在导出前将数值转为对应格式的字符串,再设置列为文本格式:

use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class YourExport implements WithColumnFormatting
{
    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_TEXT,
        ];
    }

    public function collection()
    {
        return YourModel::all()->map(function($item) {
            $value = $item->target_field;
            // 若数值有两位小数则保留,否则原样转换为字符串
            if (is_numeric($value) && floor($value) != $value) {
                $parts = explode('.', $value);
                if (strlen($parts[1]) == 2) {
                    $item->target_field = number_format($value, 2, '.', '');
                } else {
                    $item->target_field = (string)$value;
                }
            }
            return $item;
        });
    }
}

内容的提问来源于stack exchange,提问作者parthvi shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:55:27