PHP code example of zhaqq / xlsx
1. Go to this page and download the library: Download zhaqq/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/ */
zhaqq / xlsx example snippets
$writer = new Builder();
$fileName = __DIR__ . '/data/xlsx_writer' . date('Ymd-His') . '.xlsx';
$writer->buildHeader('sheet_name_1', [
'title' => 'string',
'content' => 'string',
'weight' => 'number',
]);
$writer->buildHeader('sheet_name_2', [
'title' => 'string',
'content' => 'string',
'price' => 'price',
]);
foreach (rows(100) as $row) {
$writer->writeSheetRow($row[0], $row[1]);
}
$writer->writeToFile($fileName);
function rows($n = 100)
{
for ($i = 0; $i < $n; $i++) {
if ($i % 2) {
yield ['sheet_name_1', [
'title' . $i,
'content' . $i,
$i++,
]];
} else {
yield ['sheet_name_2', [
'title' . $i,
'content' . $i,
$i++,
]];
}
}
}
try {
$writer = new Builder();
$fileName = __DIR__ . '/data/xlsx_writer' . date('Ymd-His') . '.xlsx';
$writer->buildHeader('sheet_name_1', [
'title' => 'number',
'content' => 'number',
'weight' => 'number',
]);
foreach (rows() as $row) {
$writer->writeSheetRow($row[0], $row[1]);
}
times($start);
$writer->writeToFile($fileName);
times($start);
} catch (\Exception $exception) {
var_dump($exception->getMessage());
}
function rows($n = 20)
{
for ($i = 0; $i < $n; $i++) {
yield ['sheet_name_1', [
$i,
$i,
'=A{n}+B{n}',
]];
}
}
aqq\Xlsx\Writer\Builder;
date_default_timezone_set('PRC');
try {
$writer = new Builder();
$fileName = __DIR__ . '/data/xlsx_writer' . date('Ymd-His') . '.xlsx';
$writer->buildHeader('sheet_name_1', [
'title' => 'string',
'content' => 'string',
'weight' => 'number',
]);
$writer->buildHeader('sheet_name_2', [
'title' => 'string',
'content' => 'string',
'price' => 'price',
]);
foreach (rows() as $row) {
$writer->writeSheetRow($row[0], $row[1]);
}
$writer->writeToFile($fileName);
} catch (\Exception $exception) {
var_dump($exception->getMessage());
}
function rows($n = 100)
{
for ($i = 0; $i < $n; $i++) {
if ($i % 2) {
yield ['sheet_name_1', [
'title' . $i,
'content' . $i,
$i++,
]];
} else {
yield ['sheet_name_2', [
'title' . $i,
'content' . $i,
$i++,
]];
}
}
}