Conectar con Excel:
use PhpOffice\PhpSpreadsheet\IOFactory; require_once "PHPExcel/vendor/autoload.php";
Copiar
Array cos rexistros:
$ruta=".\Lista.xls"; $documento=IOFactory::load($ruta); $hoja=$documento->getSheet(0); $filas=$hoja->getHighestDataRow(); for ($i=2; $i < $filas; $i++) { $celda1=$hoja->getCell("A".$i); $celda2=$hoja->getCell("B".$i); $celda3=$hoja->getCell("C".$i); $celda4=$hoja->getCell("D".$i); $celda5=$hoja->getCell("E".$i); echo "
$celda1
$celda2
$celda3
$celda4
$celda5
"; }
Copiar
Editar xlsm:
Usamos este comando:
composer require phpoffice/phpspreadsheet
require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\IOFactory; $spreadsheet = IOFactory::load("file.xlsm"); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World'); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('file_modified.xlsm');
Copiar
Obtener valor fecha:
require_once 'PHPExcel/Classes/PHPExcel.php'; // Carga el archivo Excel $objPHPExcel = PHPExcel_IOFactory::load("nombre_del_archivo.xlsx"); // Selecciona la hoja de trabajo activa $objWorksheet = $objPHPExcel->getActiveSheet(); // Obtiene el valor de la celda que contiene la fecha $cellValue = $objWorksheet->getCell('A1')->getValue(); // Formatea el valor como fecha $date = PHPExcel_Style_NumberFormat::toFormattedString($cellValue, 'YYYY-MM-DD'); // Imprime el resultado echo "La fecha en la celda A1 es: " . $date;
Copiar