将 XLSX 文件导入 PHP 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13439411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import an XLSX file into a PHP array
提问by ddfgdfdfgdfgdfgfdgfdgdg
Is it possible to import each line of an XLSX file to a row in a PHP array?
是否可以将 XLSX 文件的每一行导入 PHP 数组中的一行?
回答by billynoah
You can use PHPExcel which is available here: https://phpexcel.codeplex.com/releases/view/119187
您可以使用此处提供的 PHPExcel:https://phpexcel.codeplex.com/releases/view/119187
Here is what I use to read either xlsor xlsxto an array:
这是我用来读取xls或读取xlsx数组的内容:
require_once('/path/to/PHPExcel.php');
$filename = "example.xlsx";
$type = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filename);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheets[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($worksheets);
回答by coderama
回答by ns16
Problem can be solved using PHPExcellibrary:
问题可以使用PHPExcel库解决:
$data = [];
$type = PHPExcel_IOFactory::identify($filepath);
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filepath);
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
foreach($rowIterator as $row){
$cellIterator = $row->getCellIterator();
foreach ($cellIterator as $cell) {
$data[$row->getRowIndex()][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
where $filepath - path to your xls or xlsx file.
其中 $filepath - xls 或 xlsx 文件的路径。

