1. Go to this page and download the library: Download pocketframe/excel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pocketframe / excel example snippets
declare(strict_types=1);
namespace App\Excel\Imports;
use Pocketframe\Excel\Contracts\ImporterInterface;
use App\Entities\User;
class UsersImporter implements ImporterInterface
{
public function map(array $row): array
{
// Map raw row data to your data structure.
return [
'name' => $row[0],
'email' => $row[1],
'age' => (int)$row[2],
];
}
public function handle(array $data): void
{
// Process each mapped row,
// for example by saving a new User entity.
foreach ($data as $row) {
$users = new User([
'name' => $row['name'],
'email' => $row['email'],
'age' => $row['age'],
]);
$users->save();
}
}
}
use \Pocketframe\Excel\Excel;
use \App\Excel\Imports\UsersImporter;
$path = $request->file('file')->store('uploads');
Excel::import(UsersImporter::class, $path, 1000, 'user_sheet');
> public function handle(array $data): void
> {
> foreach ($data as $row) {
> $existing_user = (new QueryEngine(User::class))
> ->where('email', '=', $row['email'])
> ->first();
>
> if ($existing_user) {
> continue;
> }
>
> $category = new Category([
> 'category_name' => $row['category_name'],
> 'slug' => $row['slug'],
> 'description' => $row['description'],
> 'status' => $row['status'],
> ]);
> $category->save();
> }
> }
### Exporting Data
Generating an exporter by running the following command:
declare(strict_types=1);
namespace App\Excel\Exports;
use Pocketframe\Excel\Contracts\ExporterInterface;
use PhpOffice\PhpSpreadsheet\Style\Border;
use App\Entities\User;
use Pocketframe\PocketORM\Database\QueryEngine;
class UsersExporter implements ExporterInterface
{
public function headings(): array
{
return ['Name', 'Email', 'Age'];
}
public function data(): array
{
// Retrieve data from your entities.
return array_map(function($user) {
return [
$user->name,
$user->email,
$user->age
];
}, QueryEngine::for(User::class)->get()->all());
}
public function styles(): ?array
{
// Optional: Apply styling to the header row.
return [
'A1:C1' => [
'font' => [
'bold' => true,
'size' => 12,
'color' => [
'argb' => 'FF0000FF'
]
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
'color' => ['argb' => 'FF000000']
]
],
]
];
}
public function sheets(): ?array
{
// Return null for single-sheet export.
return null;
}
}
public function configureSheet(Worksheet $sheet): void
{
// Auto-size all columns from A through E.
foreach (range('A', 'E') as $columnID) {
$sheet->getColumnDimension($columnID)->setAutoSize(true);
}
// Determine the highest row with data.
$highestRow = $sheet->getHighestRow();
// Loop through each data row (assuming headers are in row 1).
for ($row = 2; $row <= $highestRow; $row++) {
// Get the value of the "Status" cell (column D).
$statusCell = $sheet->getCell("D{$row}");
$status = strtolower(trim($statusCell->getValue()));
if ($status === 'active') {
// Apply a light green fill for "active" status.
$sheet->getStyle("D{$row}")
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFB6D7A8');
} elseif ($status === 'inactive') {
// Apply a light red/pink fill for "inactive" status.
$sheet->getStyle("D{$row}")
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFF4B7B2');
}
}
// Optionally, apply a border style to all data cells.
$sheet->getStyle("A2:E{$highestRow}")->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFCCCCCC']
],
],
]);
}
declare(strict_types=1);
namespace App\Exports;
use Pocketframe\Excel\Contracts\ExporterInterface;
use PhpOffice\PhpSpreadsheet\Style\Border;
use App\Entities\User;
use Pocketframe\PocketORM\Database\QueryEngine;
class MultiSheetUsersExporter implements ExporterInterface
{
public function sheets(): ?array
{
return [
[
'name' => 'Active Users',
'headings' => [
'Name',
'Email',
'Age'
],
'data' => array_map(function($user) {
return [
$user->name,
$user->email,
$user->age
];
}, QueryEngine::for(User::class)
->where('status', 'active')
->get()
->all()),
'styles' => [
'A1:C1' => [
'font' => [
'bold' => true,
'size' => 12,
'color' => ['argb' => 'FF0000FF']
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
'color' => ['argb' => 'FF000000']
]
],
]
],
],
[
'name'=> 'Inactive Users',
'headings' => [
'Name',
'Email',
'Age'
],
'data' => array_map(function($user) {
return [
$user->name,
$user->email,
$user->age
];
}, QueryEngine::for(User::class)
->where('status', 'inactive')
->get()
->all()),
],
];
}
// do not remove these methods
public function headings(): array { return []; }
public function data(): array { return []; }
public function styles(): ?array { return null; }
}
use \App\Excel\Exports\MultiSheetUsersExporter;
use \Pocketframe\Excel\Excel;
Excel::export(MultiSheetUsersExporter::class)->download('users.xlsx');