PHP code example of mnb / mnb-phpexcel-xlsx

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

    

mnb / mnb-phpexcel-xlsx example snippets


use Mnb\PHPExcel\Format\Xlsx;

$rows = Xlsx::read('report.xlsx')
    ->sheet('Data')
    ->withHeaderRow()
    ->toArray();

Xlsx::write($rows, 'report-copy.xlsx', ['with_header' => true]);

Xlsx::writeImportTemplate([
    'name' => ['header' => 'Name', 'active']],
], 'import-template.xlsx');

$session = Xlsx::read('report.xlsx');

$session->sheet(1);        // first worksheet
$session->sheet('Data');   // worksheet by name
$session->sheetNames();    // inspect available names

use Mnb\PHPExcel\Support\MnbExcelException;

try {
    $rows = Xlsx::read('report.xlsx')
        ->sheet('Missing')
        ->withHeaderRow()
        ->toArray();
} catch (MnbExcelException $e) {
    print_r($e->toErrorArray(debug: true));
}

$session = Xlsx::read('report.xlsx');

if (!$session->hasSheet('Orders')) {
    $session = $session->activeSheet();
} else {
    $session = $session->sheet('Orders');
}

echo $session->activeSheetName();
print_r($session->activeSheetInfo());

$data = $session->withHeaderRow(1);
if ($data->isEmpty()) {
    // The worksheet has zero data rows after the header/options are applied.
    return;
}

$data->assertHasRows();
foreach ($data->rows() as $row) {
    // Process data.
}

$quick = Xlsx::metaInfo('report.xlsx', ['profile' => 'quick']);
$full = Xlsx::read('report.xlsx')->metaInfo(['profile' => 'full']);

Xlsx::updateMetaInfo('source.xlsx', 'updated.xlsx', [
    'document' => ['title' => 'Annual Report', 'creator' => 'MNB'],
    'custom_properties' => ['Project ID' => 'PRJ-1001'],
    'workbook' => ['active_sheet' => 'Summary'],
    'calculation' => ['mode' => 'auto'],
]);

Xlsx::removePersonalInfo('updated.xlsx', 'clean.xlsx');