我正在使用 PhpSpreadsheet 库 ( https://github.com/PHPOffice/PhpSpreadsheet ) 来读取上传的 excel 文件。excel 文件包含 html 标记和 html 特殊字符。当我的函数遍历单元格时,我得到了错误的结果。
示例代码:
$fileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFile);
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($fileType);
$objReader->setinputencoding('ISO-8859-1');
$objReader->setReadDataOnly(true);
$spreadsheet = $objReader ->load($inputFile);
foreach ($spreadsheetUploaded->getWorksheetIterator() as $worksheet) {
$array = $worksheet->toArray();
}
var_dump($array);
输出:
array(2) { [0]=> array(1) { [0]=> string(20) "cell_1,cell_2,cell_3" } [1]=> array(1) { [0]=> string(480) "
Heading – 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
,," } }
预期输出:
array(2) { [0]=> array(3) { [0]=> string(6) "cell_1" [1]=> string(6) "cell_2" [2]=> string(6) "cell_3" } [1]=> array(3) { [0]=> string(472) "
Heading - 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
" [1]=> NULL [2]=> NULL } }
请注意,单元格 1 包含 html 特殊字符“-”,相当于 '–+分号'
使用的 Excel 文件:https ://docs.google.com/spreadsheets/d/1IdLJsEmnIXiL0xPEl0J2np0fLP9gl41twk3yNHl3DzI/edit?usp=sharing
文件格式:Csv