Class 'PhpOffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet' 未在 Laravel 中找到
Class 'PhpOffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet' not found in Laravel
在我的 Laravel 项目中,我使用 PhpOffice Spreadsheet
从数据表中以 excel 格式下载数据。
为此,我需要在我的控制器、路由和视图中添加代码。
控制器
我在我的控制器中使用了以下命名空间
<?php
namespace App\Http\Controllers;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
use App\OfferCustomersDataTbl;
public function export($type) {
$offer_customer_data = OfferCustomersDataTbl::all();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Id');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Email');
$rows = 2;
foreach($offer_customer_data as $empDetails){
$sheet->setCellValue('A' . $rows, $empDetails['id']);
$sheet->setCellValue('B' . $rows, $empDetails['name']);
$sheet->setCellValue('C' . $rows, $empDetails['email']);
$rows++;
}
$fileName = "emp.".$type;
if($type == 'xlsx') {
$writer = new Xlsx($spreadsheet);
} else if($type == 'xls') {
$writer = new Xls($spreadsheet);
}
$writer->save("export/".$fileName);
header("Content-Type: application/vnd.ms-excel");
return redirect(url('/')."/export/".$fileName);
}
路线
Route::get('/export/{type}', 'OfferCustomersDataController@export');
查看
<a href="{{ url('/') }}/export/xlsx" class="btn btn-info">Export to .xlsx</a>
但是当我点击Download as Excel
按钮进行下载时,总是报错
Class 'PhpOffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet' not
found
但是 Spreadsheet
class 就在那个位置,我检查过好几次了。
我想不通的问题在哪里
有人帮忙吗?提前致谢
在命令行中:
composer require phpoffice/phpspreadsheet
然后在控制器中:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
使用'namespace'\'classname',不是绝对的link
在我的 Laravel 项目中,我使用 PhpOffice Spreadsheet
从数据表中以 excel 格式下载数据。
为此,我需要在我的控制器、路由和视图中添加代码。
控制器
我在我的控制器中使用了以下命名空间
<?php
namespace App\Http\Controllers;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
use App\OfferCustomersDataTbl;
public function export($type) {
$offer_customer_data = OfferCustomersDataTbl::all();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Id');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Email');
$rows = 2;
foreach($offer_customer_data as $empDetails){
$sheet->setCellValue('A' . $rows, $empDetails['id']);
$sheet->setCellValue('B' . $rows, $empDetails['name']);
$sheet->setCellValue('C' . $rows, $empDetails['email']);
$rows++;
}
$fileName = "emp.".$type;
if($type == 'xlsx') {
$writer = new Xlsx($spreadsheet);
} else if($type == 'xls') {
$writer = new Xls($spreadsheet);
}
$writer->save("export/".$fileName);
header("Content-Type: application/vnd.ms-excel");
return redirect(url('/')."/export/".$fileName);
}
路线
Route::get('/export/{type}', 'OfferCustomersDataController@export');
查看
<a href="{{ url('/') }}/export/xlsx" class="btn btn-info">Export to .xlsx</a>
但是当我点击Download as Excel
按钮进行下载时,总是报错
Class 'PhpOffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet' not found
但是 Spreadsheet
class 就在那个位置,我检查过好几次了。
我想不通的问题在哪里
有人帮忙吗?提前致谢
在命令行中:
composer require phpoffice/phpspreadsheet
然后在控制器中:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
使用'namespace'\'classname',不是绝对的link