PhpOffice使用笔记

PhpSpreadsheet

读取csv

以下代码可以避免乱码和读取混乱

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$reader->setInputEncoding('GBK');
$reader->setDelimiter(',');
$reader->setEnclosure('');
$reader->setSheetIndex(0);

$spreadsheet = $reader->load("file.csv");
$worksheet = $spreadsheet->getActiveSheet();
foreach ($worksheet->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    $rowData = [];
    foreach ($cellIterator as $cell) {
        $rowData[] = $cell->getValue();
    }
    dump($rowData);
}
posted @ 2024-06-20 10:18  小枫同学  阅读(14)  评论(0编辑  收藏  举报