noblemfd
1 month ago

Download sample excel sheet file before import/Upload

Posted 1 month ago by noblemfd

I have already written the code to Import/Upload Excel sheet into the database using Laravel Maatwebsite package as shown in this code.

Controller

class HrEmployeesController extends Controller
{
    public function importExportView()
    {
       return view('import');
    }

    public function import() 
    {
      Excel::import(new EmployeesImport,request()->file('file'));

      return back();
    }
}

class EmployeesImport implements ToModel, WithHeadingRow
{
   public function model(array $row)
  {
    return new HrEmployee([
      'employee_code'     => $row['employee_code'],
      'email'    => $row['email'], 
      'first_name'    => $row['first_name'],
      'last_name'    => $row['last_name'],
      'line_manager_id'    => $row['line_manager_id'],
      'employee_designation_id'    => $row['employee_designation_id'],
      'employee_job_title_id'    => $row['employee_job_title_id'],
   ]);
  }
}

View

<div class="container">
    <div class="card bg-light mt-3">
        <div class="card-header">
            Import Export Excel
        </div>
        <div class="card-body">
            <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <input type="file" name="file" class="form-control">
                <br>
                <button class="btn btn-success">Import Employee Data</button>
            </form>
        </div>
    </div>
</div>

I have been able to import the excel file successfully. However, there is one more thing. I have a sample Excel file. Before import, I want the user to download the sample excel sheet and see how the file format is, and how it should be arranged.

The file is in this directory: public/storage/employees/employee.xlsx

How do I modify my code, expecially in the view to achieve this?

Thank you

Please sign in or create an account to participate in this conversation.