2012-11-18 26 views

回答

11

您可以使用PHPExcel这可以在这里找到:https://phpexcel.codeplex.com/releases/view/119187

下面是我用什么可以读取这两种xlsxlsx到一个数组:

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); 
+0

'$ objPHPExcel = PHPExcel_IOFactory :: load(“test.xlsx”);' 这将允许您加载文件并让PHPExcel自动发现文件类型。此答案中的代码手动指定Excel 2007或5,而此代码更通用。 [来源](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md) – Matthew

+1

@Matthew - 迟了几年,但更新了自动检测答案文件类型。 – billynoah

9

我用这个:

include 'simplexlsx.class.php'; 
$xlsx = @(new SimpleXLSX('myFile.xlsx')); 
$data = $xlsx->rows(); 

你可以从here simplexslx。

UPDATE

显然上面的链接不工作了。您现在可以使用this。 (感谢@Basti)

+2

链接不再工作,但我发现这个:https://github.com/shuchkin/simplexlsx感谢提示! – Basti

1

问题可以用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(); 
    } 
} 

其中$文件路径 - 路径您的XLS或XLSX文件。