Laravel Excel html to excel conversion not working

和自甴很熟 提交于 2019-12-20 07:10:18

问题


I'm using Laravel 5.6 version and laravel-excel.maatwebsite version 3.1. I want to convert html to excel file.

In official documentation they have not given enough information to convert html views to excel. I'm having hard time.

Below is the Code for App\Exports\AttendanceExport

    <?php

          namespace App\Exports;

          use App\Models\Student\StudentAttendenceModel;
          use Illuminate\Contracts\View\View;
          use Maatwebsite\Excel\Concerns\FromView;
          use Maatwebsite\Excel\Concerns\FromCollection;

          class AttendanceExport implements FromView {
                  protected  $data = [];

                  public function __construct($data) {
                          $this->data = $data;
                  }

                  public function view(): View {                         
                          return view($this->data['file_path'], [
                                    'data' => $this->data
                          ]);
                  }

          }

MY CONTROLLER CODE : Reports\ReportsFormProcessController

    <?php

  namespace App\Http\Controllers\Reports;

  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use Illuminate\Support\Facades\Response;
  use Illuminate\Support\Facades\Redirect;
  use App\Models\Student\StudentAttendenceModel;
  //EXCEL
  use App\Exports\AttendanceExport;
  use Maatwebsite\Excel\Facades\Excel; 

    class ReportsFormProcessController extends Controller {
          public function __construct() {
                  parent::__construct();
          }     

            protected function processStdAttendance($data) {
                $excel_query = [
                    'file_name' => 'test.xlsx',
                    'sheet_name' => 'sheet 11',
                    'file_path' => 'reports.excel_templates.test',
                    'query_results' => $info_array['query_results'], //THIS AS A RESULT FROM DB
                ];

                $attendanceExport = new AttendanceExport($excel_query);
                return $attendanceExport->view();
            }

    }

MY HTML TEMPLATE CONTAINS A TABLE BELOW

    <!DOCTYPE html>
<html>
        <title>HTML Tutorial</title>
        <body>

                <table style="width:100%">
                        <tr>
                                <th>Firstname</th>
                                <th>Lastname</th> 
                                <th>Age</th>
                        </tr>
                        <tr>
                                <td>Jill</td>
                                <td>Smith</td> 
                                <td>50</td>
                        </tr>
                        <tr>
                                <td>Eve</td>
                                <td>Jackson</td> 
                                <td>94</td>
                        </tr>
                </table>
        </body>
</html>

I'M NOT GETTING ANY ERRORS BY ALSO I'M NOT GETTING EXCEL FILE DOWNLOAD

THANKS IN ADVANCE

/* EDITS */ THIS BELOW CODE STORES EXCEL FILE IN STORAGE/APP FOLDER.

return (new AttendanceExport($excel_query))->store('student_attendance.xlsx');

BUT THE DOCUMENT CODE BELOW DOES NOT DOWNLOAD THE EXCEL FILE.

return Excel::download(new AttendanceExport($excel_query), 'student_attendance.xlsx');// THIS DOES NOT DOWNLOAD 

来源:https://stackoverflow.com/questions/53778681/laravel-excel-html-to-excel-conversion-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!