nimmneun / onesheet
OneSheet 是一个快速且轻量级的单/多工作表 Excel/xlsx 文件写入器,适用于 PHP 5.4+、PHP 7 及 PHP 8,支持样式和单元格自动调整大小。
1.2.5
2023-08-01 06:23 UTC
Requires
- php: >=5.4.4
- ext-zip: *
Requires (Dev)
- phpunit/phpunit: ^4
Suggests
- ext-gd: Required for accurate cell width sizing only.
- ext-mbstring: Required for cell width auto sizing
- dev-master
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 0.7.1
- 0.7.0
- 0.6.12
- 0.6.11
- 0.6.10
- 0.6.9
- 0.6.8
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.1
- 0.1.0
- dev-maintenance/update-ci-toolchain
This package is auto-updated.
Last update: 2024-08-30 01:32:05 UTC
README
OneSheet 是一个简单的单/多工作表 Excel/xlsx 文件写入器,适用于 PHP 5、PHP 7 及 PHP 8,支持单元格自动调整大小和样式。
它能做什么
- 快速且低内存占用地写入一个或多个电子表格。
- 冻结前[n]行,以便每张表都有一个固定的表头。
- 在行级别上使用不同的字体、样式、边框和背景颜色。
- 为每列设置自己的自定义列宽。
- 自动调整列宽以适应单元格内容。如果没有找到字体,则使用粗略估计。
- 定义最小和最大列宽,以控制异常大或小的单元格内容。
它不能做什么
- 没有单元格个性化,所有操作都在行级别上应用。
- 没有计算/公式单元格。
- 没有条件格式。
- 没有数字格式。
- 没有图表。
通过 composer 安装
$ composer require nimmneun/onesheet
手动安装
如果您无法使用或不想使用 composer,请下载并解压 onsheet,然后从发布根目录中要求 autoload.php 文件。
<?php // path to onesheet autoload file on your server / webspace e.g.: require_once '/srv/fancydomain.com/libs/onesheet/autoload.php';
最小化工作示例
<?php require_once '../vendor/autoload.php'; $onesheet = new \OneSheet\Writer('/optional/fonts/directory'); $onesheet->addRow(array('hello', 'world')); $onesheet->writeToFile('hello_world.xlsx');
可用的写入器操作
Writer::setFreezePaneCellId(string $cellId)
Writer::setPrintTitleRange(int $startRow, int $endRow)
Writer::switchSheet(string $sheetName)
Writer::setFixedColumnWidths(array $columnWidths)
Writer::setColumnWidthLimits(float $minWidth, float $maxWidth)
Writer::enableCellAutosizing()
Writer::disableCellAutosizing()
Writer::addRows(array $rows, Style $style)
Writer::addRow(array $row, Style $style)
Writer::writeToFile(string $fileName)
Writer::writeToBrowser(string $fileName)
添加字体样式
Style::setFontName(string $name)
Style::setFontSize(int $size)
Style::setFontColor(string $color)
Style::setFontBold()
Style::setFontItalic()
Style::setFontUnderline()
Style::setFontStrikethrough()
添加背景颜色(填充)
Style::setFillColor(string $color)
添加边框
Style::setSurroundingBorder(string $style, string $color)
Style::setBorderLeft(string $style, string $color)
Style::setBorderRight(string $style, string $color)
Style::setBorderTop(string $style, string $color)
Style::setBorderBottom(string $style, string $color)
Style::setBorderDiagonalUp(string $style, string $color)
Style::setBorderDiagonalDown(string $style, string $color)
单元格自动调整大小
... 很酷,但会带来沉重的性能影响 - 尤其是在处理类似 ä、ß、Æ、ポ这样的多字节字符时。
但请注意 ... 您可以通过在添加了大量行之后禁用它来提高大型数据集的运行时间。
Intel Xeon E3-1220, Debian GNU/Linux 9.13, PHP 7.2.27-1+020200123.34+debian91.gbp63c0bc
其他示例
<?php require_once '../vendor/autoload.php'; // create 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__));
写入多个工作表
<?php require_once '../vendor/autoload.php'; $boldHeader = (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();
问题、错误、功能和...
请随时报告任何发现 =)。