PHP code example of xlsxwriter / excel

1. Go to this page and download the library: Download xlsxwriter/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/ */

    

xlsxwriter / excel example snippets




use XLSXWriter\ExcelWriter;

$excelWriter = new ExcelWriter();

// Suppose you want to override the default font size, type, and background color:
$customHeaderStyle = [
    'font' => [
        'size' => 20,          // bigger font
        'name' => 'Calibri',   // change to Calibri
        'color' => [
            'argb' => 'FF0000FF' // optional: change font color (blue)
        ]
    ],
    'fill' => [
        'startColor' => [
            'argb' => 'FFFFEEEE' // pale background color
        ]
    ]
];

// Set headers (automatically styled)
$excelWriter->setHeaders(['Name', 'Age', 'Email'], $customHeaderStyle);

// Add data rows
$excelWriter->addRow(['John Doe', 30, '[email protected]']);
$excelWriter->addRow(['Jane Doe', 25, '[email protected]']);

// Save the Excel file to disk
$filename = 'output.xlsx';
if ($excelWriter->write($filename)) {
    echo "Excel file created successfully at {$filename}.";
} else {
    echo "Error creating Excel file.";
}

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use XLSXWriter\ExcelWriter;

class ExcelController extends Controller
{
    public function downloadExcel()
    {
        // Initialize XLSXWriter
        $excelWriter = new ExcelWriter();

        // Suppose you want to override the default font size, type, and background color:
        $customHeaderStyle = [
            'font' => [
                'size' => 20,          // bigger font
                'name' => 'Calibri',   // change to Calibri
                'color' => [
                    'argb' => 'FF0000FF' // optional: change font color (blue)
                ]
            ],
            'fill' => [
                'startColor' => [
                    'argb' => 'FFFFEEEE' // pale background color
                ]
            ]
        ];

        $excelWriter->setHeaders(['Name', 'Age', 'Email'], $customHeaderStyle)
                    ->addRow(['John Doe', 30, '[email protected]'])
                    ->addRow(['Jane Doe', 25, '[email protected]']);

        // Write the file to disk
        $filename = 'download.xlsx';
        if ($excelWriter->write($filename)) {
            // Return file as a download and delete after sending
            return response()->download($filename)->deleteFileAfterSend(true);
        }

        return response("Error creating Excel file.", 500);
    }
}

use XLSXWriter\ExcelWriter;

$excelWriter = new ExcelWriter();

// Set the offset to (3, 3) => D4
$excelWriter->setOffset(3, 3);

// Suppose you want to override the default font size, type, and background color:
$customHeaderStyle = [
    'font' => [
        'size' => 20,          // bigger font
        'name' => 'Calibri',   // change to Calibri
        'color' => [
            'argb' => 'FF0000FF' // optional: change font color (blue)
        ]
    ],
    'fill' => [
        'startColor' => [
            'argb' => 'FFFFEEEE' // pale background color
        ]
    ]
];

// Define headers and rows
$excelWriter->setHeaders(['Fecha', 'Total'], $customHeaderStyle);
$excelWriter->addRow(['2023-01-01', '1000']);
$excelWriter->addRow(['2023-01-02', '1500']);

// Save the Excel file to disk
$filename = 'offset_example.xlsx';
if ($excelWriter->write($filename)) {
    echo "Excel file created successfully at {$filename}.";
} else {
    echo "Error creating Excel file.";
}

// Optional: If you implement a getter for the spreadsheet:
$spreadsheet = $excelWriter->getSpreadsheet();
$spreadsheet->getActiveSheet()->getStyle('A1:C100')->applyFromArray([
    'font' => [
        'name' => 'Calibri',
        'size' => 11
    ]
]);