馬克貝克非常有助於指導我正確的答案。我不使用作曲家與PHP(我應該學習),但考慮到,爲了得到這個工作,我去了PHPExcel的GitHub頁面(https://github.com/PHPOffice/PHPExcel),點擊綠色克隆並下載按鈕,然後下載ZIP鏈接。
解壓縮文件後,我得到一個名爲PHPExcel-1.8
的文件夾。我將該文件夾移動到與我想要讀取的Excel文件(在我的代碼爲test.xlsx
以下)和具有下面代碼的PHP文件相同的文件夾中。
讓它工作的關鍵是輸入IOFactory.php
文件的正確路徑。有些人可能看起來很簡單,但卻讓我感到沮喪。
鑑於上述情況和Mark Baker的意見,下面的代碼工作完美的我(注意註釋部分):
<?php
//Had to change this path to point to IOFactory.php.
//Do not change the contents of the PHPExcel-1.8 folder at all.
include('PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');
//Use whatever path to an Excel file you need.
$inputFileName = 'test.xlsx';
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' .
$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 1; $row <= $highestRow; $row++) {
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
null, true, false);
//Prints out data in each row.
//Replace this with whatever you want to do with the data.
echo '<pre>';
print_r($rowData);
echo '</pre>';
}
PHPExcel已經從20移動到21世紀。使用composer進行安裝,或者做一個git pull(不要試圖選擇你想要的單個文件),或者至少點擊「克隆或下載」按鈕並以zip形式下載。 –
[使用PHPExcel將數據插入數據庫的示例](http://stackoverflow.com/questions/9695695/how-to-use-phpexcel-to-read-data-and-insert-into-database) –
謝謝爲指針,馬克。稍微玩了一下之後,我才得以開始工作。我會發布我做的答案。謝謝。 – HartleySan