2012-11-18 49 views

Trả lời

11

Bạn có thể sử dụng PHPExcel trong đó có sẵn ở đây: https://phpexcel.codeplex.com/releases/view/119187

Đây là những gì tôi sử dụng để đọc hoặc xls hoặc xlsx để một mảng:

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 ");' Điều này sẽ cho phép bạn tải tệp và PHPExcel tự động phát hiện loại tệp. Mã trong câu trả lời này tự xác định Excel 2007 hoặc 5 trong khi mã này linh hoạt hơn. [source] (https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md) – Matthew

+1

@Matthew - một vài năm trễ nhưng cập nhật câu trả lời này để tự động phát hiện loại tệp. – billynoah

9

tôi sử dụng này:

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

Bạn có thể simplexslx từ here.

CẬP NHẬT

Rõ ràng liên kết ở trên không hoạt động nữa. Bây giờ bạn có thể sử dụng this. (Cảm ơn @Basti)

+2

Liên kết không hoạt động nữa, nhưng tôi thấy điều này: https://github.com/shuchkin/simplexlsx cảm ơn gợi ý! – Basti

1

Vấn đề có thể được giải quyết bằng PHPExcel thư viện:

$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(); 
    } 
} 

nơi $ filepath - đường dẫn đến xls hay xlsx.

Các vấn đề liên quan