You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel控制器修改需求:将数据导出为Excel替代PDF

Switching from PDF to Excel Export in Your Laravel Controller

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.getUsageReport blade view uses a simple HTML table—Excel handles table formatting best.
  • You can rename usage_report.xlsx to any filename you prefer.

内容的提问来源于stack exchange,提问作者timah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:16:02