該擴展包可讓你輕松讀取和寫入簡單的 Excel 和 CSV 檔案,在后臺使用生成器來確保低記憶體使用,即使在處理大型檔案時也是如此,
這是有關如何讀取 Excel 或 CSV 的示例,
SimpleExcelReader::create($pathToFile)->getRows()
->each(function(array $rowProperties) {
// process the row
});
如果 $ pathToFile 以 .csv 結尾,則假定為 CSV 檔案,如果以 .xlsx 結尾,則假定為 Excel 檔案,
安裝
你可以通過 composer 安裝該軟體包:
$ composer require spatie/simple-excel
用法
讀取 CSV
想象你有一個包含如下內容的 CSV 檔案
email,first_name [email protected],john [email protected],jane
// $rows是 Illuminate\Support\LazyCollection 的一個實體
$rows = SimpleExcelReader::create($pathToCsv)->getRows();
$rows->each(function(array $rowProperties) {
// 回圈的第一個 $rowProperties 應該是下面這樣的
// ['email' => 'john@example', 'first_name' => 'john']
});
讀取 Excel 檔案
讀取 Excel 檔案與讀取 CSV 檔案相同,只需確保提供給 SimpleExcelReader 的 create 方法的路徑以 xlsx 結尾,
使用懶集合
getRows 將回傳 LazyCollection 實體,該實體是 Laravel 框架的一部分,因為在后臺使用了生成器,即使是大檔案記憶體使用量也會較低,
你可以在這里. 找到關于 LazyCollection 的方法
這是一個簡單的愚蠢的例子,我們只想處理 first_name 長度大于 5 的行,
SimpleExcelReader::create($pathToCsv)->getRows()
->filter(function(array $rowProperties) {
return strlen($rowProperties['first_name']) > 5
})
->each(function(array $rowProperties) {
// processing rows
});
讀取一個沒有標題的檔案
如果你要讀取一個沒有標題的檔案,你應該使用 noHeaderRow()
// $rows是 Illuminate\Support\LazyCollection 的一個實體
$rows = SimpleExcelReader::create($pathToCsv)
->noHeaderRow()
->getRows()
->each(function(array $rowProperties) {
// 第一次回圈的 $rowProperties 會是下面這樣
// [0 => 'john@example', 1 => 'john']
});
自己創建一個閱讀器
首先我們已經引入了 box/spout 這個包, 你可以通過 getReader 方法獲取一個閱讀器的介面 \Box\Spout\Reader\ReaderInterface
$reader = SimpleExcelReader::create($pathToCsv)->getReader();
寫入檔案
這里將展示如何寫入一個 CSV 檔案:
$writer = SimpleExcelWriter::create($pathToCsv)
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
])
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
])
;
pathToCsv 檔案將包含以下內容:
first_name,last_name John,Doe Jane,Doe
寫入 Excel 檔案
寫入 Excel 檔案與寫入 CSV 相同,只需確保提供給 SimpleExcelWriter 的 create 方法的路徑以 xlsx 結尾,
將 Excel 檔案流式傳輸到瀏覽器
無需將檔案寫入磁盤,您可以將其直接流式傳輸到瀏覽器,
$writer = SimpleExcelWriter::streamDownload('your-export.xlsx')
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
])
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
])
->toBrowser();
寫入沒有標題的檔案
如果正在寫入的檔案沒有標題行,則應使用 noHeaderRow() 方法,
$writer = SimpleExcelWriter::create($pathToCsv)
->noHeaderRow()
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
]);
});
這將輸出:
Jane,Doe
添加布局
這個包底層使用了 box/spout 包,該軟體包包含一個 StyleBuilder ,可用于格式化行,請注意樣式只能在 Excel 檔案上使用,
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Common\Entity\Style\Color; $style = (new StyleBuilder()) ->setFontBold() ->setFontSize(15) ->setFontColor(Color::BLUE) ->setShouldWrapText() ->setBackgroundColor(Color::YELLOW) ->build(); $writer->addRow(['values, 'of', 'the', 'row'], $style)
有關樣式的更多資訊,請查閱 Spout 檔案.
使用替代定界符
默認情況下, SimpleExcelReader 將假定分隔符為 ,,
使用其他分隔符的方法:
SimpleExcelWriter::create($pathToCsv)->useDelimiter(';');
獲取寫入的行數
您可以獲取寫入的行數,該數字包括自動添加的標題行,
$writerWithAutomaticHeader = SimpleExcelWriter::create($this->pathToCsv)
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
]);
$writerWithoutAutomaticHeader->getNumberOfRows() // returns 2
手動使用 writer 物件
因基于 box/spout 包,所以你可以通過 getWriter 來獲取到底層的 \Box\Spout\Reader\WriterInterface 實作:
$writer = SimpleExcelWriter::create($pathToCsv)->getWriter();
更多學習內容請訪問:
騰訊T3-T4標準精品PHP架構師教程目錄大全,只要你看完保證薪資上升一個臺階(持續更新)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67438.html
標籤:PHP
上一篇:laravel的中間件創建思路
下一篇:PHPExcel實作匯入匯出功能
