Skip to content

Instantly share code, notes, and snippets.

@csalgueiro
Created May 31, 2017 08:50
Show Gist options
  • Save csalgueiro/3bc59e85c525b6d72cd134e0a337c7bb to your computer and use it in GitHub Desktop.
Save csalgueiro/3bc59e85c525b6d72cd134e0a337c7bb to your computer and use it in GitHub Desktop.
Cargar un Template XLSX y cubrir los valores
<?
require_once './lib/PHPExcel/Classes/PHPExcel.php';
$fileName = './templates/excel/plantilla.xlsx';
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objPHPExcel->load($fileName);
$active_sheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$row = 0;
$row_inicio = 0;
$col_inicio = 0;
$highestColumnNumber = PHPExcel_Cell::columnIndexFromString($highestColumn) - 2;
// Algo que recorra todos los valores para obtener los nombres de los campos que haya en la template
for ($r = $row_inicio; $r <= $highestRow; $r++) {
$column = 0;
for ($c = $col_inicio; $c < $highestColumnNumber; $c++) {
$value = $worksheet->getCellByColumnAndRow($c, $r)->getFormattedValue();
$datos[$row][$column] = $value;
///si es un nombre de campo
$value = $obj->$value;
// Algo que cubra los valores
$active_sheet->setCellValue($colLetra . $row, $value);
$column++;
}
$row++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filePath = '../uploads/tmp/' . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
$objWriter->save($filePath);
readfile($filePath);
unlink($filePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment