PHP code example of nimmneun / onesheet

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

    

nimmneun / onesheet example snippets



// path to onesheet autoload file on your server / webspace e.g.:



heet = new \OneSheet\Writer('/optional/fonts/directory');
$onesheet->addRow(array('hello', 'world'));
$onesheet->writeToFile('hello_world.xlsx');



eate a header style
$headerStyle = (new \OneSheet\Style\Style())
    ->setFontSize(13)
    ->setFontBold()
    ->setFontColor('FFFFFF')
    ->setFillColor('777777');

// create a data style
$dataStyle1 = (new \OneSheet\Style\Style())
    ->setFontName('Segoe UI')
    ->setFontSize(10);

// create a second data style
$dataStyle2 = (new \OneSheet\Style\Style())
    ->setFontName('Arial')
    ->setFillColor('F7F7F7');

// prepare some dummy header data
$dummyHeader = array('Strings', 'Ints', 'Floats', 'Dates', 'Times', 'Uids');

// prepare some dummy data
$dummyData = array();
for ($i = 1; $i <= 100; $i++) {
    $dummyData[] = array(
        substr(md5(microtime()), rand(11,22)),
        rand(333,333333),
        microtime(1),
        date(DATE_RSS, time() + $i*60*60*24),
        date('H:i:s', time() + $i),
        uniqid('', true)
    );
}

// create new OneSheet instance
$onesheet = new \OneSheet\Writer();

// add header with style
$onesheet->addRow($dummyHeader, $headerStyle);

// freeze everything above cell A2 (the first row will be frozen)
$onesheet->setFreezePaneCellId('A2');

// enable autosizing of column widths and row heights
$onesheet->enableCellAutosizing();

// add dummy data row by row and switch between styles
foreach ($dummyData as $key=> $data) {
    if ($key % 2) {
        $onesheet->addRow($data, $dataStyle1);
    } else {
        $onesheet->addRow($data, $dataStyle2);
    }
}

// ignore the coming rows for autosizing
$onesheet->disableCellAutosizing();

// add an oversized dummy row
$onesheet->addRow(array('no one cares about my size and I dont even have a special style!'));

// add the all the dummy rows once more, because we can =)
$onesheet->addRows($dummyData);

// Override column widths for columns 6, 7, 8 (column 0 is the first)
$onesheet->setFixedColumnWidths(array(5 => 10, 6 => 10, 7 => 10));

// write everything to the specified file
$onesheet->writeToFile(str_replace('.php', '_onesheet.xlsx', __FILE__));



Header = (new OneSheet\Style\Style())->setFontBold();

// create initial writer instance with sheet name
$writer = new \OneSheet\Writer(null, 'Invoices');
$writer->enableCellAutosizing(); // enable for current sheet
$writer->addRow(['InvoiceNo', 'Amount', 'CustomerNo'], $boldHeader);
$writer->addRow(['']); // add empty row bcs fancy :D
$writer->addRow(['I-123', 123.45, 'C-123']);

// create new sheet with specific sheet name
$writer->switchSheet('Refunds');
$writer->enableCellAutosizing(); // enable for current sheet
$writer->addRow(['RefundNo', 'Amount', 'InvoiceNo'], $boldHeader);
$writer->addRow(['']); // add empty row bcs fancy :D
$writer->addRow(['R-123', 123.45, 'I-123']);

// create another sheet with specific sheet name
$writer->switchSheet('Customers');
$writer->enableCellAutosizing(); // enable for current sheet
$writer->addRow(['CustomerNo', 'FirstName', 'LastName'], $boldHeader);
$writer->addRow(['']); // add empty row bcs fancy :D
$writer->addRow(['C-123', 'Bob', 'Johnson']);

// send file to browser for downloading 
$writer->writeToBrowser();