1// Include PHPExcel_IOFactory
2include 'PHPExcel/IOFactory.php';
3
4$inputFileName = './sampleData/example1.xls';
5
6// Read your Excel workbook
7try {
8 $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
9 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
10 $objPHPExcel = $objReader->load($inputFileName);
11} catch(Exception $e) {
12 die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
13}
14
15// Get worksheet dimensions
16$sheet = $objPHPExcel->getSheet(0);
17$highestRow = $sheet->getHighestRow();
18$highestColumn = $sheet->getHighestColumn();
19
20// Loop through each row of the worksheet in turn
21for ($row = 1; $row <= $highestRow; $row++){
22 // Read a row of data into an array
23 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
24 NULL,
25 TRUE,
26 FALSE);
27 // Insert row data array into your database of choice here
28}
29
1Mark Baker was extremely helpful in guiding me to the right answer. I don't use Composer with PHP (I should probably learn), but given that, in order to get this to work I went to the GitHub page for PHPExcel (https://github.com/PHPOffice/PHPExcel), clicked the green Clone and download button, and then the Download ZIP link.
2
3After unzipping the file, I got a folder called PHPExcel-1.8. I moved that folder to the same folder as both the Excel file I wanted to read (in my code below test.xlsx) and the PHP file that has the code below.
4
5The key to getting it to work was inputting the correct path to the IOFactory.php file. It may seem simple to some, but it was tripping me up.