Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Using the PHPExcel library to read an Excel file and transfer the data into a database

//  Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';

$inputFileName = './sampleData/example1.xls';

//  Read your Excel workbook
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());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);
    //  Insert row data array into your database of choice here
}
Comment

how do i use php read excel file

Mark 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.

After 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.

The 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.
Comment

php read excel file

I use PHP-ExcelReader to read xls files, and works great.
Comment

PREVIOUS NEXT
Code Example
Php :: php file_get_contents html with special characters 
Php :: where is in array laravel 
Php :: laravel basic login 
Php :: if file is not selected in file input type php 
Php :: Laravel - Add conditional where clause in query 
Php :: Laravel check for constraint violation 
Php :: how to create cookie in laravel 
Php :: laravel factory pass parameter 
Php :: laravel edit form modal example 
Php :: display php error 
Php :: how to refresh php page automatically 
Php :: get woocommerce my account page url 
Php :: array_map in php 
Php :: upload image to database laravel 8 
Php :: laravel where and blade 
Php :: php creating a subdomain automatically in cpanel 
Php :: php input onchange 
Php :: install all php extensions ubuntu 20.04 
Php :: limit query codeiniter 3 
Php :: append single quote around variable in php string 
Php :: woocommerce unset custom checkout field 
Php :: php capture include 
Php :: install composer laravel 
Php :: Regullar date format for php 
Php :: yii2 activeform adding field css class 
Php :: php timezone paris 
Php :: php pdo delete 
Php :: laravel 7 upload file s3 
Php :: switching between php versions 
Php :: php fpdf in phpmailer 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =