首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Excel中的文本字段在Laravel中作为数字导入?

为什么Excel中的文本字段在Laravel中作为数字导入?
EN

Stack Overflow用户
提问于 2019-12-12 15:15:19
回答 1查看 751关注 0票数 1

我试图将Excel表格导入Laravel,但Excel中的文本字段作为数字导入。将数据导入到Excel中(text = text和number=number)是偏离了目标的。

Excel: 屏幕截图

的结果是Laravel: 屏幕截图

链接到屏幕截图,无法显示内联图像。

我使用https://laravel-excel.com/https://novapackages.com/packages/anaseqal/nova-import

ContactsImport.php

代码语言:javascript
复制
<?php

namespace App\Imports;

use App\Contact;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ContactsImport implements ToCollection
{

public function collection(Collection $rows)
{
    foreach ($rows as $row) {
        Contact::create([
        'first_name' => $row[0],
        'last_name' => $row[1],
        'address' => $row[2],
        'postal_code' => $row[3],
        'city' => $row[4],
        'country' => $row[5],
        'cellphone' => $row[6],
        'email' => $row[7],
        ]);
    }
}

}

ImportContacts.php

代码语言:javascript
复制
namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Anaseqal\NovaImport\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use Laravel\Nova\Fields\File;

use App\Imports\ContactsImport;
use Maatwebsite\Excel\Facades\Excel;

class ImportContacts extends Action
{
use InteractsWithQueue, Queueable, SerializesModels;

/**
 * Indicates if this action is only available on the resource detail view.
 *
 * @var bool
 */
public $onlyOnIndex = true;

/**
 * Get the displayable name of the action.
 *
 * @return string
 */
 public function name() {
    return __('Import Contacts');
 }

/**
 * @return string
 */
public function uriKey() :string
{
    return 'import-contacts';
}

/**
 * Perform the action.
 *
 * @param  \Laravel\Nova\Fields\ActionFields  $fields
 * @param  \Illuminate\Support\Collection  $collections
 * @return mixed
 */
public function handle(ActionFields $fields, Collection $collections)
{
    Excel::import(new ContactsImport, $fields->file);
    return Action::message('The contacts have been imported.');
}

/**
 * Get the fields available on the action.
 *
 * @return array
 */
public function fields()
{
    return [
        File::make('File')
            ->rules('required'),
    ];
}
}

几个小时以来,我一直在寻找模拟物,但什么也找不到。

EN

回答 1

Stack Overflow用户

发布于 2020-01-11 01:19:44

通过读取此文档,确保您正在按其名称键获取该列,然后再次尝试var_dump($rows),以确保您从excel文件中获得了正确的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59307681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档