close
轉自 https://learnku.com/articles/42615
--
Excel/CSV 導入導出庫,支持大文件,樣式設置
前言
- 項目上使用過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
enabledR
## 安裝
- composer require rocky114/spreadsheet
- https://github.com/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();
本作品採用《CC協議》,轉載必須註明作者和本文鏈接
--
全站熱搜
留言列表