【问题标题】:Column name getting changed while importing column from excel in laravel excel在 laravel excel 中从 excel 导入列时更改列名
【发布时间】:2020-06-09 06:42:27
【问题描述】:

您好,我正在使用 laravel excel 从 excel 导入文件并获取数据,但列标题正在更改,我需要保持不变。例如:列名是 Mat_Type-1 然后它被转换为 mat_type_1。

而我想保持相同以从列中获取一些数据。

控制器中的功能:

public function uploadexcel(Request $request){
// $this->validate($request, [
//  'importfile'  => 'required|mimes:xls,xlsx'
// ]);
    echo $path = $request->file('importfile');

    $array = Excel::toArray(new ProductsImport, $path);


    print_r($array);
}

Excel\Imports 类:

命名空间 App\Imports;

  use App\Models\Admin\CarVariant;
  use App\Models\Admin\Product;
  use Illuminate\Support\Carbon;
  use Illuminate\Support\Facades\DB;
  use Illuminate\Validation\Rule;
  use Maatwebsite\Excel\Concerns\ToModel;
  use Maatwebsite\Excel\Concerns\WithBatchInserts;
  use Maatwebsite\Excel\Concerns\WithChunkReading;
  use Maatwebsite\Excel\Concerns\WithHeadingRow;
  use Maatwebsite\Excel\Concerns\WithValidation;
 class ProductsImport implements ToModel,WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
            // dd($row);

}

}

非常感谢任何帮助。尝试了多个选项但不起作用。

【问题讨论】:

  • @ahackney 列名是动态的,它会改变想要读取该列名并使用相同的名称。有一个 HeadingRowFormatter 选项,但对如何实现它感到困惑。

标签: php excel laravel phpexcel laravel-excel


【解决方案1】:

试试这个。它来自一个现场项目。我将文件保存到一个文件夹并获取数组中的数据,然后将其按到数据表中。数据表名称没有变化。

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

public function import(Request $request)
{
    //Save the File
    if($request->hasFile('file'))
    {
        // Get the file with extension
        $filenameWithExt = $request->file('file')->getClientOriginalName();
        //Get the file name
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        //Get the ext
        $extension = $request->file('file')->getClientOriginalExtension();
        //File name to store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        //Upload File
        $path = $request->file('file')->storeAS('public/excels', $fileNameToStore);


    }


    $this->validate($request, [
        'file' => 'required|mimes:xlsx,xls',
    ]);

    $products = Excel::toArray(new ProductsImport(), $request->file('file'));

    foreach($products[0] as $row) {
        //  dd($row[1].' '.$row[2]);
        $arr[] = [
            // If uncomment this id from here, remove [0] from foreach
            // 'id' => $row[0], 
            'image' => $row[1],
            'title' => $row[2],
            'slug' => $row[3],
            'text' => $row[4],
            'brand_id' => $row[5],
            'category_id' => $row[6],
        ];
    }

    if(!empty($arr)){
        DB::table('products')->insert($arr);
    }

    return back()->with('success', 'Excel file uploaded to database successfully');

}

【讨论】:

  • 列名是动态的,它会改变想要读取该列名并使用相同的名称。有一个 HeadingRowFormatter 选项,但对如何实现它感到困惑
【解决方案2】:

因此,无论您对行做什么,这都应该有效。

我会假设您正在为 Excel 文件的每一行创建一个新模型。

 public function model(array $row)
    {
        return new Whatever([
            'Mat_Type-1'  => $row['mat_type_1'],
            'Column_Name-1'  => $row['column_name_1'],
            'Column_Name-2'  => $row['column_name_2'],
        ]);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2021-07-21
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多