5

I am trying to import data from excel sheet(.xlsx). I have found PHPExcel for import data but after downloading document and source code I am confused which file is important for me. I also tried to find out document on that site but not found the way.

About my task: Read excel sheet data from selected sheet and insert data to my database table.

So I really thankful If You will guide me how to use it.

Thanks.

3 Answers 3

12

You can use the PHPExcel library to read an Excel file and insert the data into a database.

Sample code is below.

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

$inputFileName = 'sample.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 database here using your own structure
2
  • A better answer than transferring the entire worksheet to a PHP array; iterate over each row in turn so you're only working with a small array of data and you're not going to blow PHP's memory limit
    – Mark Baker
    Commented Oct 12, 2014 at 8:45
  • In case you know the format of the data before hand, e.g. a user upload, you can set $highestColumn to the last column of data such as $highestColumn = 'E' in my own case. This is useful as your database will generally have fixed columns.
    – seyisulu
    Commented Apr 7, 2017 at 5:07
8

From a quick look over the documentation, use the IOFactory to automatically determine the file format.

include 'path/to/PHPExcel/IOFactory.php';

// Let IOFactory determine the spreadsheet format
$document = PHPExcel_IOFactory::load('path/to/spreadsheet.xls');

// Get the active sheet as an array
$activeSheetData = $document->getActiveSheet()->toArray(null, true, true, true);

var_dump($activeSheetData);
0

Try this so you can jump start the development process:

include '.... /PHPexcel/Classes/PHPExcel.php';
    $dataFfile = "C:/tmp/test_data.xls";
    $objPHPExcel = PHPExcel_IOFactory::load($dataFfile);
    $sheet = $objPHPExcel->getActiveSheet();
    $data = $sheet->rangeToArray('A2:AB5523');
    echo "Rows available: " . count($data) . "\n";
    foreach ($data as $row) {
        print_r($row);
    }

Replace the include path with your path to the PHPExcel.php
Replace $dataFile with your excel file
Also adjust the range of the cells that you want to import

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.