conceptsandtraining / lib-excel-wrapper
v0.4.1
2024-03-28 15:00 UTC
Requires
- box/spout: 3.2.*
Requires (Dev)
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-master
- sensiolabs/security-checker: ^4.1
This package is not auto-updated.
Last update: 2024-09-20 02:09:56 UTC
README
一个用于流式Excel写入器的包装器。
联系人: Richard Klees
写入文件
Excel写入必须实现定义的功能。
- 创建写入对象
- 在任何时候定义单列的样式
- 按行填充工作表
- 创建新工作表并通过名称切换
- 保存到指定的文件夹和文件名
按行填充工作表是推荐的功能,可以提高大量数据导出的速度。将无法回退到行或单列来更改值或其他内容。
public function addRow(array $values) { $this->writer->addRow($values); }
在设计工作表时,可以随时为任何列应用新的样式。例如,在行1到5中,列A是加粗并左对齐的。从行6开始,它将只左对齐,不再加粗。这为设计列标题或突出特殊值提供了机会。
public function setColumnStyle(Style $style, $column) { $this->writer->setColumnStyle($column, $style); };
可以创建新工作表并通过名称切换。如果您创建了一个新工作表,它将自动成为当前工作表。
public function createSheet($name) { $this->writer->createSheet($name); } Public function selectSheet($name) { $this->writer->setCurrentSheet($name); }
定义样式
列可以通过多种方式进行样式设置。此包装器包含大多数常用的样式。
- 字体家族
- 字体大小
- 加粗
- 斜体
- 下划线
- 文本颜色
- 背景颜色
- 方向
- 边框
- 边框颜色
public function getNewStyle() { $style = new Style(); $style ->setFontFamily('Aarial') ->setFontSize(12) ->setItalic(false) ->setBorder(Style::TOP); return $style; }
使用示例
这是一个使用此包装器的小示例。
public function exportData($file_name, $file_path, array $header, array $values) { $writer = new Writer(); $writer->setFileName($file_name); $writer->setPath($file_path); $header_style = $this->getHeaderStyle(); $writer->setColumnStyle('A', $header_style); $write->addRow($header); $bold_style = $this->getBoldStyle(); $basic_style = $this->getBasicStyle(); $writer->setColumnStyle('A', $bold_style); $writer->setColumnStyle('B', $basic_style); foreach($values as $value) { $write->addRow($value); } $writer->setColumnStyle('A', $basic_style); $writer->setColumnStyle('B', $bold_style); foreach($values as $value) { $write->addRow($value); } $writer->saveFile(); $write->close(); } protected function getHeaderStyle() { $style = new Style() $style->setBold(true) ->setItalic(true); return $style; } protected function getBoldStyle() { $style = new Style() $style->setBold(true) return $style; } protected function getBsicStyle() { return new Style(); }
生成的xlsx工作表可能看起来像这样。