我试图将Excel表格导入Laravel,但Excel中的文本字段作为数字导入。将数据导入到Excel中(text = text和number=number)是偏离了目标的。
Excel: 屏幕截图
的结果是Laravel: 屏幕截图
链接到屏幕截图,无法显示内联图像。
我使用https://laravel-excel.com/和https://novapackages.com/packages/anaseqal/nova-import
ContactsImport.php
<?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
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'),
];
}
}几个小时以来,我一直在寻找模拟物,但什么也找不到。
发布于 2020-01-11 01:19:44
通过读取此文档,确保您正在按其名称键获取该列,然后再次尝试var_dump($rows),以确保您从excel文件中获得了正确的值。
https://stackoverflow.com/questions/59307681
复制相似问题