Index Column for Laravel Excel Maatwebsite - excel

I want to export the data from my database to excel using laravel excel maatwebsite, the problem is i don't know how to add index column to the excel file. for example i want the data in excel from this look:
Name Birthdate
Rudolf 1990-05-20
Andi 1991-11-01
Harry 1993-03-25
to this:
No Name Birthdate
1 Rudolf 1990-05-20
2 Andi 1991-11-01
3 Harry 1993-03-25
this is my model:
namespace App\Exports;
use App\user;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class userExport implements FromQuery, WithHeadings, ShouldAutoSize{
use Exportable;
public function headings(): array{
return [
'Tanggal',
'Nomor Permohonan',
'Nama Pemohon',
];
}
public function __construct(int $status)
{
$this->status = $status;
}
public function query(){
$date = \Carbon\Carbon::now()->toDateString() ;
return user::query()->select('name','birthdate')->where('status', $this->status)->whereDate('date',$date);
}
}
My function in controller:
public function export() {
return (new userExport(1))->download('user.xlsx');
}
How can i do that?, help me. Thank you.

You have to try select id in your query user::query()->select('id', 'name','birthdate')

Related

Read specific sheet by name on import excel laravel

I have an excel with multiple sheets but i only want read one sheet:
My problem is that i have multiple sheets, and they do not have the same order or the same number of pages. Then, i must identify the sheet by name on my import class (laravel import).
Here is my import class:
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
class ExecutedUPS implements WithMultipleSheets, WithEvents
{
private $nameSheetUPS = "LMS - F";
public function sheets(): array
{
$sheets = [];
//doesn't work for me
if($event->getSheet()->getTitle() === $this->nameSheetUPS){
$sheets[] = new SheetUPS();
}
return $sheets;
}
}
And here is my class "SheetUPS":
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class SheetUPS implements ToCollection
{
/**
* #param Collection $collection
*/
public function collection(Collection $collection)
{
//this i know do
}
}
Have you tried selecting the sheet by name as the documentation does?
//class ExecutedUPS
public function sheets(): array
{
return [
'LMS - F' => new SheetUPS(),
];
}

Maatwebsite / laravel give name to Worksheet in export

I am using Maatwebsite / laravel to export data , I need to rename the Worksheet ,
below my class :
<?php
namespace App\Exports;
use App\Models\Client;
use App\Models\Item;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
class ItemExport implements FromCollection, WithHeadings, ShouldAutoSize
{
protected $rows;
use Exportable;
public function __construct($client_id , $items , $company_name)
{
$this->client_id = $client_id;
$this->items = $items;
$this->company_name = $company_name;
}
public function collection()
{
$i = 1;
$client = Client::find($this->client_id);
$item_new= [];
foreach($this->items as $item){
$item_new[] = [$i++,
$this->company_name,
$this->item,
$client->client_name,
];
}
return collect($item_new);
}
public function title(): string
{
return $this->company_name;
}
public function headings(): array
{
return [
'#',
'Company',
'item',
'client',
];
}
}
but it seems the public function title(): string , not working .
i supposed that the title function will work , but seems no
any idea how to give name to Worksheet ?
thanks
So close. You just need to implement the WithTitle interface.
class ItemExport implements FromCollection, WithHeadings, WithTitle, ShouldAutoSize
An example is somewhat buried in the multiple sheets section of the documentation, but I can confirm it works for single sheets too.
https://docs.laravel-excel.com/3.1/exports/multiple-sheets.html#sheet-classes
Laravel-How to create multiple sheet in one excel file using maatwebsite/excel": "^3.1.
1st step: I created a multiple excel export file
php artisan make:export MultipleSheetExport
2nd Create a route and controller
In controller I have written this function
public function state_summary_export()
{
return Excel::download(new MultipleSheetExport, 'ADCC Summary.xlsx');
}
Note: 'ADCC Summary.xlsx' is my excel file name.
Don’t forget to add this facades in you controller
use App\Exports\MultipleSheetExport;
use Maatwebsite\Excel\Facades\Excel;
3rd step: Open MultipleSheetExport
<?php
namespace App\Exports;
use Illuminate\Support\Arr;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithTitle;
class MultipleSheetExport implements WithMultipleSheets
{
public function sheets(): array
{
$sheets = [
new StateSummaryExport(),
new BMRPerformanceExport(),
];
return $sheets;
}
}
4th step: Go to Child StateSummaryExport and add this given line.
class StateSummaryExport implements WithTitle
public function title(): string
{
return 'State Summary';
}
Note: ‘State Summary’ is your sheet name. Do the same in BMRPerformanceExport.
Thank you.

How to read data from the specific sheet number from Maatwebsite/Laravel-Excel package?

I am working on import excel data in Laravel framework and using Maatwebsite/Laravel-Excel package.
Now I have followed the steps but not getting any idea after reading package documentation, so I need help to read data from the second(2nd) sheet as I have multiple sheets in my excel file.
BillingDataImport
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class BillingDataImport implements ToCollection
{
/**
* #param Collection $collection
*/
public function collection(Collection $collection)
{
return $collection;
}
}
BillingController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\BillingDataImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Input;
class BillingController extends Controller
{
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$import = new BillingDataImport();
$rawData = Excel::toArray($import, $request->file('company_1'));
var_dump($rawData);
}
}
When I dump the code, I am getting data from the first(1st) sheet but I want to read second(2nd) sheet so can you please guide me as I am not too good Laravel and with this package.
You need to implement WithMultipleSheets interface in your import class. It will require you to add sheets method. You will include them like these;
public function sheets(): array
{
return [
0 => new FirstSheetImport(),
1 => new SecondSheetImport(),
];
}
Then you may select them by index. More info here

How can apply style using withEvents in multiplesheets in maatwebsite 3.1?

I have used laravel excel version 2.1 previously . In version 2.0 cell designing was pretty easy . So i started a new project in latest edition of laravel (6) and was shocked to know that we can't use laravel excel version 2.0 in this version of laravel . I installed latest version of laravel-excel 3.1 and sheet styling is totally different from the version larave-excel version 2.0 so i dig into documentation i found this custom styles using registerEvents for the multiple sheets but it didn't work.
Here is my Export class.
<?php
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Excel;
use App\Exports\ExportSheet;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Sheet;
class ExcelExport implements WithMultipleSheets, WithEvents
{
use Exportable;
/**
* #var Invoice $invoice
*/
protected $excel_input;
private $writerType = 'xlsx';
public function __construct($excel_input)
{
$this->excel_input = $excel_input;
}
public function sheets(): array
{
$data = $this->excel_input;
$sheets = [];
foreach($data as $sheet_data) {
$sheets[] = new ExportSheet($sheet_data);
}
return $sheets;
}
public function registerEvents(): array
{
$normal_style = array('font' => array('name' => 'Times New Roman','size' => 15));
return [
AfterSheet::class => function(AfterSheet $event) use($normal_style) {
$event->sheet->getStyle("A:Z")->getAlignment()->setWrapText(true);
$event->sheet->getStyle("A:Z")->applyFromArray($normal_style);
$event->sheet->setAutoSize(true);
$event->sheet->setScale('100');
$event->sheet->setFitToHeight(false);
$event->sheet->setFitToWidth(true);
$event->sheet->setPageMargin(0.25);
},
];
}
}
Here is my ExportSheet file for generate multiple sheets.
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Illuminate\Contracts\View\View;
use PhpOffice\PhpSpreadsheet\Style\Style;
class ExportSheet implements WithTitle, ShouldAutoSize, FromView, WithColumnFormatting
{
private $sheet_data;
public function __construct(array $sheet_data)
{
$this->sheet_data = $sheet_data;
}
/**
* #return string
*/
public function title(): string
{
$data = $this->sheet_data;
return $data['sheet_name'];
}
public function ShouldAutoSize()
{
return true;
}
public function view(): View
{
$info = $this->sheet_data;
return view($info['template_name'])->with(['data'=>$info]);
}
}```
I had same problem. the registerEvents worked fine without implements WithMultipleSheets. But after implements WithMultipleSheets, it didn't work anymore.
Is there anyone give me some advices please?

"Class 'App\Http\Controllers\InvoicesExport' not found" in Maatwebsite Laravel

In my Laravel Project i use Maatwebsite\Excel for to export Data in Excel format. i use New Version 3.0 (Maatwebsite\Excel)
App/Exports.php
namespace App\Exports;
use App\Purchasepaymenttransaction;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class InvoicesExport implements FromView
{
public function view(): View
{
$purchasepayment=Purchasepaymenttransaction::Where('transaction_category',2)->OrderBy('transaction_date','DESC')->get();
return view('exports.purchasepayments', [
'purchasepayment' => $purchasepayment
]);
}
}
In my Payment Controller
<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Purchasepaymenttransaction;
use Excel;
use App\Exports;
class PaymentController extends Controller
{
public function purchaseexport()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
}
i got "Class 'App\Http\Controllers\InvoicesExport' not found" Error
return Excel::download(new \InvoicesExport, 'invoices.xlsx');
Normally you use backslash('\') before class when you get class not found exception. It will automatically find that class from your code and use it where you need and don't require to use it at top of file. It is similar to
use [YOUR_PATH]/InvoicesExport
If you use "use" statement then no need to backslash('\'). You can use any of above two. It will work.
use App\Exports\InvoicesExport as ExportsInvoicesExport;

Resources