项目中用到了 Laravel Excel 导出数据(https://github.com/SpartnerNL/Laravel-Excel),但是在遇到身份证号或手机号这种长数字时,导出的数据会自动转为科学计数法,实际上是不符合我们的需求的,解决方法是使用 Value binders :

use PhpOfficePhpSpreadsheetCellCell;
use PhpOfficePhpSpreadsheetCellDataType;
use PhpOfficePhpSpreadsheetCellDefaultValueBinder;

class ExamineesExport extends DefaultValueBinder implements FromQuery,WithCustomValueBinder
{
    use Exportable;

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

    //设置导出格式,如果字段长度大于10,那么设置成 TYPE_STRING ,否则调用 DefaultValueBinder 的默认设置
    public function bindValue(Cell $cell, $value)
    {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }

        return parent::bindValue($cell, $value);
    }

    public function query()
    {
        return Examinee::query()->where('project_id',$this->project_id);
    }
}