Skip to content

Instantly share code, notes, and snippets.

@brunotdantas
Created November 22, 2018 12:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brunotdantas/3cb56a11667531980185be3ffda2a4d5 to your computer and use it in GitHub Desktop.
Save brunotdantas/3cb56a11667531980185be3ffda2a4d5 to your computer and use it in GitHub Desktop.
Create a spreadsheet with phpSpreadSheet (https://github.com/PHPOffice/PhpSpreadsheet) #Excel #xls #xlsx
<?php
// requiring autoload with the pluguin
require __DIR__ . '/../../vendor/autoload.php';
// invoking needed classes
use PhpOffice\PhpSpreadsheet\Spreadsheet; //class responsible to change the spreadsheet
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; //class that write the table in .xlsx
$spreadsheet = new Spreadsheet(); //instanciando uma nova planilha
$sheet = $spreadsheet->getActiveSheet(); //retornando a aba ativa
// Populating the table
$sheet->setCellValue('A1', 'Nome'); //Definindo a célula A1
$sheet->setCellValue('B1', 'Nota 1'); //Definindo a célula B1
$sheet->setCellValue('C1', 'Nota 2');
$sheet->setCellValue('D1', 'Media');
$sheet->setCellValue('A2', 'pokemaobr');
$sheet->setCellValue('B2', 5);
$sheet->setCellValue('C2', 3.5);
$sheet->setCellValue('D2', '=((B2+C2)/2)'); //Definindo a fórmula para o cálculo da média
$sheet->setCellValue('A3', 'bob');
$sheet->setCellValue('B3', 7);
$sheet->setCellValue('C3', 8);
$sheet->setCellValue('D3', '=((B3+C3)/2)');
$sheet->setCellValue('A4', 'boina');
$sheet->setCellValue('B4', 9);
$sheet->setCellValue('C4', 9);
$sheet->setCellValue('D4', '=((B4+C4)/2)');
$writer = new Xlsx($spreadsheet); //Instance of a new spreadsheet
$filename = 'tabela'.'.xlsx'; // naming the table
$writer->save("../download/$filename"); //writing the table into a file
$file_location = 'http://localhost:81/portal/src/download/'. $filename; // defining the download SRC link + table name
?>
<!-- donwloading the table that was created from a button -->
<input type="button" value="Put Your Text Here" onclick="window.location.href='<?=$file_location?>'" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment