Laravel-admin v1.8.10版本的导出问题

6 篇文章 0 订阅
  1. 导出为中文字符串乱码的解决方案
vendor\encore\laravel-admin\src\Grid\Exporters\CsvExporter.php
在这个文件夹中修改:
/**
     * {@inheritdoc}
     */
    public function export()
    {
        if ($this->callback) {
            call_user_func($this->callback, $this);
        }
        //就是这句话,加上就不乱码了;
        print(chr(0xEF).chr(0xBB).chr(0xBF));
        $response = function () {
            $handle = fopen('php://output', 'w');
            $titles = [];

            $this->chunk(function ($collection) use ($handle, &$titles) {
                Column::setOriginalGridModels($collection);

                $original = $current = $collection->toArray();

                $this->grid->getColumns()->map(function (Column $column) use (&$current) {
                    $current = $column->fill($current);
                    $this->grid->columnNames[] = $column->getName();
                });

                // Write title
                if (empty($titles)) {
                    fputcsv($handle, $titles = $this->getVisiableTitles());
                }

                // Write rows
                foreach ($current as $index => $record) {
                    fputcsv($handle, $this->getVisiableFields($record, $original[$index]));
                }
            });
            fclose($handle);
        };

        response()->stream($response, 200, $this->getHeaders())->send();

        exit;
    }
  1. 导出为科学计数法的解决方案
/**
     * @param string $column
     * @param mixed  $value
     * @param mixed  $original
     *
     * @return mixed
     */
    protected function getColumnValue(string $column, $value, $original)
    {
        if (!empty($this->columnUseOriginalValue)
            && in_array($column, $this->columnUseOriginalValue)) {
            return $original;
        }

        if (isset($this->columnCallbacks[$column])) {
            return $this->columnCallbacks[$column]($value, $original);
        }
		// 在最后拼接“\t”即可解决
        return $value . "\t";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值