Tutorial - Aprende a leer un archivo excel y obtener sus datos con PHP

in Develop Spanish5 years ago

En el día de hoy aprenderemos a leer un archivo excel con PHP para obtener sus datos y mostrarlos en una grilla, estaremos usando como framework de css Bootstrap y la librería que nos ayudara a gestionar el excel sera PhpSpreadsheet's, en este link pueden leer acerca de su documentación https://phpspreadsheet.readthedocs.io/en/latest/

PHP

Asumiendo que ya cuentan con una distribución de apache (servidor HTTP) instalado (bien sea Wamp, Lamp, XAMP o cualquier otro) creamos una carpeta llamada php-excel/ (coloca esta carpeta dentro de C://xampp/htdocs/ ) y dentro un archivo llamado index.php

Antes de continuar es necesario instalar la libreria para luego ahcer uso de ella para ello activamos la consola/terminal y ejecutamos lo siguiente:


composer require phpoffice/phpspreadsheet

php-excel/index.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tutorial - Aprende a leer un archivo excel y obtener sus datos con PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <h1 class="text-center">Tutorial - Aprende a leer un archivo excel y obtener sus datos con PHP</h1>
        <div class="row pt-4">
            <div class="col-md-5 mx-auto">
                <div class="card">
                    <div class="card-body">
                        <form method="post" action="process.php" enctype="multipart/form-data">
                            <div class="mb-2">
                                <label for="formFile" class="form-label">Adjuntar Excel</label>
                                <input required class="form-control" type="file" id="formFile" name="file" accept=".xlsx,.csv">
                            </div>
                            <div class="d-grid gap-2 mt-5"><button type="submit" class="btn btn-primary btn-block" name="submit">Procesar</button></div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</body>
</html>

php-excel/process.php


<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
require_once 'vendor/autoload.php';
$file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if (isset($_POST["submit"])) {
    if (isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {
        $arr_file = explode('.', $_FILES['file']['name']);
        $extension = end($arr_file);
        if ('csv' == $extension) {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
        } else {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        }
        $spreadsheet = $reader->load($_FILES['file']['tmp_name']);
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn(); 
        $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); 
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tutorial - Aprende a leer un archivo excel y obtener sus datos con PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <h1 class="text-center">Tutorial - Aprende a leer un archivo excel y obtener sus datos con PHP - <code>Resultado</code></h1>
        <div class="row pt-4">
            <div class="col-md-8 mx-auto">
                <?php
                echo '<table class="table">
                    <tr>
                        <th>JUEGO</th>
                        <th>PLATAFORMA</th>
                        <th>FECHA LANZAMIENTO</th>
                        <th>GENERO</th>
                    <tr>
                ' . "\n";
                for ($row = 2; $row <= $highestRow; ++$row) {
                    echo '<tr>' . PHP_EOL;
                    for ($col = 1; $col <= $highestColumnIndex; ++$col) {
                        $value = $worksheet->getCellByColumnAndRow($col, $row)->getValue();
                        echo '<td>' . $value . '</td>' . PHP_EOL;
                    }
                    echo '</tr>' . PHP_EOL;
                }
                echo '</table>' . PHP_EOL;
                ?>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous">>/script>
</body>
</html>


Para este ejemplo se uso el siguiente archivo Excel:



Y con esa amigos llegamos al final del tutorial, espero que lo hayan disfrutado y hasta la próxima!



Visita mi sitio web oficial para presupuestos y mucho más

TupaginaOnline.net

Sort:  

Su post ha sido valorado por @ramonycajal

microscope.jpg