PHP code example of piyush / excel-importer

1. Go to this page and download the library: Download piyush/excel-importer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

piyush / excel-importer example snippets




iyush\ExcelImporter\ExcelReader;

// Load an Excel file (auto-detects format)
$excel = ExcelReader::load('/path/to/file.xlsx');

// Get all sheet names
$sheetNames = $excel->getSheetNames();
print_r($sheetNames);

// Get a specific sheet by name or index
$sheet = $excel->getSheet('Sheet1');
// or
$sheet = $excel->getSheet(0);

// Iterate through rows
foreach ($sheet->getRows() as $rowIndex => $row) {
    foreach ($row->getCells() as $colIndex => $cell) {
        $value = $cell->getValue();        // mixed (string, int, float, DateTime, bool, null)
        $type = $cell->getType();          // 'string', 'number', 'date', 'boolean', 'empty'
        $formatted = $cell->getFormattedValue();  // Always returns string

        echo "Row {$rowIndex}, Col {$colIndex}: {$formatted} ({$type})\n";
    }
}

// Quick export to array
$data = $sheet->toArray();
print_r($data);

use Piyush\ExcelImporter\ExcelReader;

// Load entire file
$workbook = ExcelReader::load('/path/to/file.xlsx');

// Load only specific sheets
$workbook = ExcelReader::loadSheets('/path/to/file.xlsx', ['Sheet1', 'Data']);

// Get sheet names without loading data
$names = ExcelReader::getSheetNames('/path/to/file.xlsx');

// Check if file can be read
$canRead = ExcelReader::canRead('/path/to/file.xlsx');

// Get sheet by name
$sheet = $workbook->getSheet('Sheet1');

// Get sheet by index (0-based)
$sheet = $workbook->getSheet(0);

// Get first sheet
$sheet = $workbook->getFirstSheet();

// Get all sheets
$sheets = $workbook->getSheets();

// Get sheet names
$names = $workbook->getSheetNames();

// Get sheet count
$count = $workbook->getSheetCount();
// or
$count = count($workbook);

// Check if sheet exists
$exists = $workbook->hasSheet('Sheet1');

// Iterate through sheets
foreach ($workbook as $sheet) {
    echo $sheet->getName();
}

// Get sheet name and index
$name = $sheet->getName();
$index = $sheet->getIndex();

// Get a specific row (1-based index)
$row = $sheet->getRow(1);

// Get all rows
$rows = $sheet->getRows();

// Get a specific cell by coordinate
$cell = $sheet->getCell('A1');
$cell = $sheet->getCell('B2');

// Get dimensions
$highestRow = $sheet->getHighestRow();
$highestCol = $sheet->getHighestColumn();        // Returns index
$highestColLetter = $sheet->getHighestColumnLetter();  // Returns letter (e.g., 'D')

// Convert to 2D array
$data = $sheet->toArray();

// Convert to associative array (first row as headers)
$data = $sheet->toAssociativeArray();

// Iterate through rows
foreach ($sheet as $row) {
    // ...
}

// Get row index (1-based)
$index = $row->getRowIndex();

// Get cell by column index (0-based)
$cell = $row->getCell(0);  // First column

// Get cell by column letter
$cell = $row->getCellByColumn('A');

// Get all cells
$cells = $row->getCells();

// Convert to array
$values = $row->toArray();

// Check if row is empty
$isEmpty = $row->isEmpty();

// Iterate through cells
foreach ($row as $cell) {
    // ...
}

// Get value (mixed type)
$value = $cell->getValue();

// Get type ('string', 'number', 'date', 'boolean', 'empty', 'formula')
$type = $cell->getType();

// Get formatted value (always string)
$formatted = $cell->getFormattedValue();

// Get coordinate
$coord = $cell->getCoordinate();  // e.g., 'A1'

// Type checking
$cell->isEmpty();      // true if empty
$cell->isString();     // true if string
$cell->isNumber();     // true if number
$cell->isDate();       // true if date
$cell->isBoolean();    // true if boolean

$cell = $sheet->getCell('A1');

if ($cell->isDate()) {
    $dateTime = $cell->getValue();  // DateTime object
    echo $dateTime->format('Y-m-d'); // 2024-01-15
}

use Piyush\ExcelImporter\Helpers\DateHelper;

// Convert Excel date to DateTime
$dateTime = DateHelper::excelToDateTime(44941);  // 2023-01-15

// Convert to timestamp
$timestamp = DateHelper::excelToTimestamp(44941);

// Convert to formatted string
$formatted = DateHelper::excelToFormatted(44941, 'Y-m-d');

$data = $sheet->toAssociativeArray();

// Result:
// [
//     ['Name' => 'John', 'Email' => '[email protected]', 'Age' => 30],
//     ['Name' => 'Jane', 'Email' => '[email protected]', 'Age' => 25],
// ]

foreach ($data as $record) {
    echo $record['Name'];
    echo $record['Email'];
}

use Piyush\ExcelImporter\ExcelReader;

$excel = ExcelReader::load('users.xlsx');
$sheet = $excel->getFirstSheet();

// Skip header row
$rows = $sheet->toAssociativeArray();

foreach ($rows as $row) {
    $db->insert('users', [
        'name' => $row['Name'],
        'email' => $row['Email'],
        'created_at' => $row['Join Date']->format('Y-m-d H:i:s')
    ]);
}