3

前言

  • 项目上使用过maatwebsite/Excel, mk-j/php_xlsxwriter, box/spout这三个库,简单说下使用体验
  • maatwebsite/Excel 优点功能非常强大,不足的是占用内存太多,对于导出大文件无法满足需求
  • mk-j/php_xlsxwriter 优点支持大文件写入,常规样式设置,不足的是仅支持excel导出,不支持导入,也不支持csv的导入/导出
  • box/spout 优点是支持大文件,支持excel/csv导入/导出,不足的是不支持单元格合并

环境要求

  • PHP version 7.1 or higher
  • PHP extension php_zip enabled
  • PHP extension php_xmlreader enabled

安装

  • composer require rocky114/spreadsheet

地址

导出

include "./vendor/autoload.php";

$writer = \Rocky114\Excel\Writer\WriterFactory::createXLSXWriter();
$writer->setTempFolder('.');

$writer->addNewSheet('sheet1');

$type = ['A' => 'string', 'B' => '#,##0'];
$writer->addHeader(['name', 'id'], $type)
->addRow(['xinzhu', 1234565])>addRow(['rocky', 21])->save();

导入

include "./vendor/autoload.php";

$reader = \Rocky114\Spreadsheet\ReaderFactory::createReaderFromFile('./test.xlsx');

$data = [];
foreach ($reader->getSheetIterator() as $sheet) {
     foreach ($sheet->getRowIterator() as $row) { 
        $data[] = $row; 
     }
}

// or
$data = $reader->getSheet()->load();

洛奇_巴布亚
23 声望2 粉丝

« 上一篇
strace
下一篇 »
快速排序