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
}

4.1
10
Masoodahm 75 points

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

4.1 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
php read data from excel file phpexcel upload excel file to database read excel and save data in php mysql import excel file into mysql database using php import excel to database php excel file read in php how too import data from excel sheet via php how to import excel file into mysql database table in php can i store files inside an excel file using php how to import and read and calculate excel file php use excel as database in php upload excel file to mysql using php import excel file data in mysql database using php how to retrieve and export data from database to excel sheet in php read data from excel file in php get data from excel file and save in the database php read excel db php excel file to table php database export data from database to excel in php how to add excel file to sql database using php import excel data to mysql database php get data from excel file in php phpexcel import file to sql oracle inport excel file into table using php how to export sql data to excel using php Read and write excel data with PHP upload excel file to database php get excel file data in php upload excel file to db in php php export excel from database php excel import to database php import data from excel import excel file to sql database php how to upload excel file to database in php how do i use php read excel file read excel file in php and insert into database upload excel file to sql server php how to import excel file to mysql in php Using the PHPExcel library to read an Excel file and transfer the data into a database
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source