如何在 laravel 中以表格格式导出带有合并单元格的 csv 数据

问题描述 投票:0回答:1

我必须在 laravel 中以 csv 格式导出数据。我使用的方式是仅导出行文件,但我想将其格式化为图像中共享的格式我想要导出csv的格式

我在这里分享控制器功能和导出文件

文件名 App\Exports\ExportCertificate.php

<?php

namespace App\Exports;

use App\Models\Certificates;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use DB;

class ExportCertificate implements FromCollection, WithHeadings
{
    /**
    * @return \Illuminate\Support\Collection
    */

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

    public function headings(): array
    {
        // Define the header row with the names of the database fields
        return [
            'Sr.No.',
            'Batch Number',
            'Batch Number Manual',
            'Exporter Name',
            'Certificate Number',
            'Certificate Date',
            'Item Name',
            'Treated QTY',
            'Invoice Number',
            'Invoice Date',
            'Country Of Export',
            'Moisture Before Treat',
            'Moisture After Treat',
            'Total Time',
            'Warmup Time',
            'Dwell Time',
            'Company Name',
            // Add more columns as needed
        ];
    }
    public function collection()
    {
        $added_by           = session('login_type');
        $added_by_id        = session('user_id');

        $Records            = Certificates::where('added_by',$added_by)
                                ->where('added_by_id',$added_by_id)
                                ->where('id',$this->id)
                                //->orderBy('id','DESC')
                                ->get();

        return $Records->map(function ($Record, $index) {

                $BatchDetails   = \App\Models\Batches::find($Record->batch_id);
                $exporters      = \App\Models\Exporterlist::find($Record->exporter_id);
                $Item       = DB::table('items')->where('id',$Record->item_id)->first();
                
                if(isset($BatchDetails->manual_batch_number))
                {
                    $manual_batch_number = $BatchDetails->manual_batch_number;
                }
                else
                {
                    $manual_batch_number = 'N/A';
                }

                if(isset($exporters->party_name))
                {
                    $exporterName = $exporters->party_name;
                }
                else
                {
                    $exporterName = 'N/A';
                }

                if(isset($Item->name))
                {
                    $ItemName = $Item->name;
                }
                else
                {
                    $ItemName = 'N/A';
                }



                return [
                    'Sr.No.'                => $index + 1,
                    'Batch Number'          => $Record->batch_id ?? 'N/A',
                    'Batch Number Manual'   => $manual_batch_number, 
                    'Exporter Name'         => $exporterName, 
                    'Certificate Number'    => $Record->certificate_number ?? 'N/A',
                    'Certificate Date'      => $Record->certificate_date ?? 'N/A',
                    'Item Name'             => $ItemName,
                    'Treated QTY'           => $Record->treated_quantity,
                    'Invoice Number'        => $Record->invoice_number,
                    'Invoice Date'          => $Record->invoice_date,
                    'Country Of Export'     => $Record->country_of_export,
                    'Moisture Before Treat' => $Record->before_moisture,
                    'Moisture After Treat'  => $Record->after_moisture,
                    'Total Time'            => $Record->total_time,
                    'Warmup Time'           => $Record->warmup_time,
                    'Dwell Time'            => $Record->dwell_time,
                    'Company Name'          => $Record->company_name,
                    // Add more columns as needed
                ];
            });
    }
}

下面是App\Http\Controllers dmin\CertificateController.php中控制器的功能

    public function export_certificates_excel ($id)
    {
        return Excel::download(new ExportCertificate($id), 'certificates.xlsx');
    }

这就是路线

Route::any('/export_certificates_excel/{id}', 'CertificateController@export_certificates_excel')->name('certificate.export_certificates_excel');
excel laravel csv export-to-csv
1个回答
0
投票

为了实现您的自定义标题格式,您的导出需要实现

WithEvents
接口:

class ExportCertificate implements FromCollection, WithHeadings, WithEvents
{
  ...
  
  public function registerEvents(): array
  {
    return [
      BeforeSheet::class => function (BeforeSheet $event) {
        $event->sheet->getDelegate()->setCellValue('B1', 'Your custom heading');
        ...
        // other headings

        // merge cells
        $event->sheet->getDelegate()->setMergeCells(['A4:M4', 'A5:H5', ...]);
      }];
  }
}

通过 setCellValue 您可以填充空标题单元格,通过 setMergeCells 您可以传递要以“$from:$to”格式合并的单元格列表

© www.soinside.com 2019 - 2024. All rights reserved.