phpspreadsheet导出Excel报错: Invalid numeric value for datatype Numeric

使用 phpspreadsheet 导出Excel的一段代码如下:

$sheet->setCellValue($cellName[$kk] . ($key + 2), $vv);

如果某个字段值里面包含 换行符(\n) 会导致报错:

Invalid numeric value for datatype Numeric

解决办法:

if (substr($vv, -1) == "\n" || substr($vv, -1) == "\t") {
     $vv .= " ";
 }
 $sheet->setCellValue($cellName[$kk] . ($key + 2), $vv);

使用phpspreadsheet 导出Excel的方法示例如下:

	/**
     * 导出Excel文件
     * @param array $list 数据列表数组
     * @param array $field_map 数组字段数组
     * @param string $file_path 文件路径
     * @param string $filename 文件名称
     * @param bool $filename_append_now_time 文件名是否追加当前时间
     * @param string $suffix 文件后缀
     * @return false|string
     * @throws Exception
     */
    public static function outputExcel(array $list, array $field_map, string $file_path, string $filename, bool $filename_append_now_time = true, string $suffix = 'xlsx')
    {
        //处理 $list 数据列表
        $data = [];
        foreach ($list ?? [] as $v) {
            $item = [];
            foreach ($field_map as $field_key => $field_val) {
                //解析 $field_map 中 数组多级key的数据,需要在 $field_map 中定义多个key 以.分隔, 例如: user.info.name
                $field_key_split = explode('.', $field_key);
                $container = $v;
                for ($index = 0; $index <= 2; $index++) {
                    if (isset($field_key_split[$index]) && isset($container[$field_key_split[$index]])) {
                        $container = $container[$field_key_split[$index]];
                    }
                }
                if (is_array($container)) {
                    $container = '';
                }
                $item[$field_key] = $container;
            }
            //$data[] = array_values($item);
            $data[] = $item;
        }

        //表头A-Z列定义
        $cellName = [];
        for ($i = 'A'; $i <= 'Z'; $i++) {
            $cellName[] = $i;
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //设置表头行
        $titleList = array_values($field_map);
        //设置表头单元格样式
        $styleArray = [
            'font' => [
                'bold' => true, // 字体加粗
                'color' => [
                    'rgb' => '0000ff',
                ],
            ]
        ];
        foreach ($titleList as $tkey => $tval) {
            $sheet->setCellValue($cellName[$tkey] . '1', $tval);
            $sheet->getStyle($cellName[$tkey] . '1')->applyFromArray($styleArray);
        }

        //设置表数据内容行和列
        foreach ($data ?? [] as $key => $val) {
            $val = array_values($val);
            foreach ($val as $kk => $vv) {
                if (is_string($vv)) { //对于字符串类型,防止较长的数字字符串被格式化为科学计数法
                    $sheet->setCellValueExplicitByColumnAndRow(($kk + 1), ($key + 2), $vv, DataType::TYPE_STRING);
                } else {
                    //导出的某个字段值里面包含 换行符(\n) 会导致报错: Invalid numeric value for datatype Numeric
                    if (substr($vv, -1) == "\n" || substr($vv, -1) == "\t") {
                        $vv .= " ";
                    }
                    $sheet->setCellValue($cellName[$kk] . ($key + 2), $vv);
                }
            }
        }

        //创建目录并写入Excel文件
        if (!is_dir($file_path)) {
            if (!mkdir($file_path, 0777, true)) {
                return false;
            }
        }
        if ($filename_append_now_time) {
            $filename .= '-' . date("YmdHis", time());
        }
        $filename .= '.' . strtolower($suffix);
        $objWriter = new Xlsx($spreadsheet);
        $objWriter->save($file_path . '/' . $filename);

        return $filename;
    }

调用部分示例:

$list = json_decode('[{"id":1,"username":"jihongchu","mobile":"177888","nickname":"王先生","avatar":"https://profile-avatar.csdnimg.cn/default.jpg","status":1,"time":{"last_login_time":"2024-03-22 14:42:14","add_time":"2024-03-22 14:42:14","update_time":"2024-03-27 10:47:57"}}]',true);

//设置导出的字段和表头
$field_map = [
    "id" => "ID",
    "username" => "用户名",
    "mobile" => "手机号",
    "nickname" => "昵称",
    "status" => "状态",
    //针对二维模型导出的字段定义
    "time.last_login_time" => "最后登录时间",
    "time.add_time" => "添加时间",
    "time.update_time" => "修改时间",
];
$file_path = $file_root . '/' . $file_folder; //绝对路径
$file_title = '用户数据导出-' . time();
$output_file_name = FileUtil::outputExcel($list, $field_map, $file_path, $file_title);

导出结果:
在这里插入图片描述

相关推荐

  1. phpspreadsheet导出数据和图片到excel

    2024-03-28 05:08:06       31 阅读
  2. PhpSpreadsheet 读取 excel 里面的图片

    2024-03-28 05:08:06       16 阅读
  3. Spark 读excel,scala.MatchError

    2024-03-28 05:08:06       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 05:08:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 05:08:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 05:08:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 05:08:06       20 阅读

热门阅读

  1. dfs (蓝桥备赛)

    2024-03-28 05:08:06       20 阅读
  2. React系列之React版本时间线和主要更新

    2024-03-28 05:08:06       25 阅读
  3. Unity3D 制作MMORPG 3D地图编辑器详解

    2024-03-28 05:08:06       22 阅读
  4. 【无标题】

    2024-03-28 05:08:06       16 阅读
  5. 内核态转发平面的SSL加速

    2024-03-28 05:08:06       19 阅读
  6. Spring_MVC

    2024-03-28 05:08:06       19 阅读
  7. BaseDao封装增删改查(超详解!)

    2024-03-28 05:08:06       20 阅读
  8. docker初识

    2024-03-28 05:08:06       21 阅读
  9. RoCE v2中UDP的源端口和目的端口

    2024-03-28 05:08:06       22 阅读