Laravel控制器修改需求:将数据导出为Excel替代PDF
Hey there! Converting your existing PDF export to Excel is straightforward with the Maatwebsite/Laravel-Excel package (the go-to tool for Excel exports in Laravel). Let's walk through the changes you need to make:
Step 1: Install Laravel Excel (if not already set up)
First, add the package via Composer:
composer require maatwebsite/excel
If you want to customize the package config later, you can publish it (optional for basic use):
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag="config"
Step 2: Update Your Controller Code
Replace the PDF generation logic with Excel export functionality. You have two simple options—either export the database collection directly, or reuse your existing blade view (just like you did for PDF). Here's the updated getUsageData method:
use Maatwebsite\Excel\Facades\Excel; use App\Exports\UsageDataExport; // We'll create this class next public function getUsageData(Request $request) { $start_date = $request->get('start_date'); $end_date = $request->get('end_date'); $particulars = DB::table('particulars') ->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id') ->whereBetween('date_applied', [$start_date, $end_date]) ->select( 'particulars.item_name', 'particulars.unit', 'particulars.price', 'reqs.quantity_issued', DB::raw('particulars.price*reqs.quantity_issued AS total_cost') ) ->get(); if ($particulars->isEmpty()) { return "No Records Found...................... "; } else { // Option 1: Use a dedicated export class (cleaner for reusability) return Excel::download(new UsageDataExport($particulars), 'usage_report.xlsx'); // Option 2: Reuse your existing blade view (skip the export class if you choose this) // return Excel::download(new class($particulars) implements FromView { // protected $particulars; // public function __construct($particulars) { // $this->particulars = $particulars; // } // public function view(): View { // return view('issuer.getUsageReport', ['particulars' => $this->particulars]); // } // }, 'usage_report.xlsx'); } }
Step 3: Create the Export Class (for Option 1)
If you go with Option 1, make a new file app/Exports/UsageDataExport.php to define your export structure:
namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; use Illuminate\Support\Collection; class UsageDataExport implements FromCollection { protected $particulars; public function __construct(Collection $particulars) { $this->particulars = $particulars; } public function collection() { // Add a custom header row for clarity (remove this if you don't want it) $header = collect([['Item Name', 'Unit', 'Price', 'Quantity Issued', 'Total Cost']]); return $header->merge($this->particulars); } }
Quick Tips:
- The
Excel::download()method automatically generates the XLSX file and triggers a browser download for the user. - If you use Option 2, ensure your
issuer.getUsageReportblade view uses a simple HTML table—Excel handles table formatting best. - You can rename
usage_report.xlsxto any filename you prefer.
内容的提问来源于stack exchange,提问作者timah

