Last active
February 23, 2018 19:40
-
-
Save FusRoDah061/df83cbeda12bd7128176374c65fefe05 to your computer and use it in GitHub Desktop.
Gerar planilha do excel em PHP através de dados do MySQL usando o PhpSpreadsheet <https://github.com/PHPOffice/PhpSpreadsheet>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Estrutura das tabelas do banco de dados: | |
CREATE TABLE pessoas ( | |
nome varchar(100) NOT NULL, | |
idade int(11) NOT NULL, | |
sexo varchar(1) NOT NULL, | |
altura double NOT NULL, | |
peso double NOT NULL | |
); | |
INSERT INTO pessoas (nome, idade, sexo, altura, peso) VALUES | |
('Allex', 19, 'm', 1.8, 70), | |
('João Paulo', 18, 'm', 1.6, 67), | |
('Nilzete', 52, 'f', 1.65, 79), | |
('Noilson', 55, 'm', 1.77, 74); | |
*/ | |
class Database{ | |
private $server = "localhost"; | |
private $user = "root"; | |
private $password = ""; | |
private $database = "test"; | |
private $bd_con = null; | |
private $is_connected = false; | |
//Abre a conexão com o banco | |
public function __construct(){ | |
$this->bd_con = mysqli_connect($this->server, $this->user, $this->password); | |
if($this->bd_con){ | |
$this->is_connected = true; | |
mysqli_select_db($this->bd_con, $this->database); | |
mysqli_set_charset($this->bd_con, "utf8"); | |
} | |
else{ | |
$this->is_connected = false; | |
} | |
} | |
//Mata a conexão com o banco | |
public function __destruct(){ | |
$this->bd_con->close(); | |
} | |
//Retorna verdadeiro se houver conexão com o banco e falso caso contrário | |
public function is_connected(){ | |
return $this->is_connected; | |
} | |
//Obtém todos os eventos do banco de dados | |
public function get_dados(){ | |
if(!$this->is_connected){ | |
return null; | |
} | |
$sql = $this->bd_con->prepare("SELECT * FROM pessoas;"); | |
if($sql){ | |
if($sql->execute()){ | |
$result = $sql->get_result(); | |
if($result){ | |
if($result->num_rows > 0){ | |
$dados = array(); | |
while($obj = $result->fetch_assoc()){ | |
array_push($dados, $obj); | |
} | |
mysqli_free_result($result); | |
return $dados; | |
} | |
} | |
} | |
} | |
return null; | |
} | |
} | |
require 'vendor/autoload.php'; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
$db = new Database(); | |
if(!$db->is_connected()){ | |
echo "Sem conexão com o banco de dados"; | |
} | |
else{ | |
$dados = $db->get_dados(); | |
if($dados){ | |
$spreadsheet = new Spreadsheet(); | |
$sheet = $spreadsheet->getActiveSheet(); | |
//Definne os cabeçalhos | |
$sheet->setCellValue('A1', "Nome"); | |
$sheet->setCellValue('B1', "Idade"); | |
$sheet->setCellValue('C1', "Sexo"); | |
$sheet->setCellValue('D1', "Altura"); | |
$sheet->setCellValue('E1', "Peso"); | |
for($i = 0; $i < sizeof($dados); $i++){ | |
$sheet->setCellValue('A'.($i + 2), $dados[$i]["nome"]); | |
$sheet->setCellValue('B'.($i + 2), $dados[$i]["idade"]); | |
$sheet->setCellValue('C'.($i + 2), $dados[$i]["sexo"]); | |
$sheet->setCellValue('D'.($i + 2), $dados[$i]["altura"]); | |
$sheet->setCellValue('E'.($i + 2), $dados[$i]["peso"]); | |
} | |
$writer = new Xlsx($spreadsheet); | |
//O arquivo será salvo na mesma pasta desse script | |
$writer->save('PessoasTesteMySQL.xlsx'); | |
} | |
$spreadsheetOff = new Spreadsheet(); | |
$sheetOff = $spreadsheetOff->getActiveSheet(); | |
//Definne os cabeçalhos | |
$sheetOff->setCellValue('A1', "Nome"); | |
$sheetOff->setCellValue('B1', "Idade"); | |
$sheetOff->setCellValue('C1', "Sexo"); | |
$sheetOff->setCellValue('D1', "Altura"); | |
$sheetOff->setCellValue('E1', "Peso"); | |
$sheetOff->setCellValue('A2', "Allex"); | |
$sheetOff->setCellValue('B2', "19"); | |
$sheetOff->setCellValue('C2', "m"); | |
$sheetOff->setCellValue('D2', "1,8"); | |
$sheetOff->setCellValue('E2', "70"); | |
$sheetOff->setCellValue('A3', "Lucas"); | |
$sheetOff->setCellValue('B3', "28"); | |
$sheetOff->setCellValue('C3', "m"); | |
$sheetOff->setCellValue('D3', "1,86"); | |
$sheetOff->setCellValue('E3', "84"); | |
$sheetOff->setCellValue('A4', "Carla"); | |
$sheetOff->setCellValue('B4', "34"); | |
$sheetOff->setCellValue('C4', "f"); | |
$sheetOff->setCellValue('D4', "1,68"); | |
$sheetOff->setCellValue('E4', "61"); | |
$writer = new Xlsx($spreadsheetOff); | |
//O arquivo será salvo na mesma pasta desse script | |
$writer->save('PessoasTeste.xlsx'); | |
echo "Excel gerado na pasta desse script.<br/> | |
<a href='http://localhost:8081/PessoasTesteMySQL.xlsx'>Baixar versão com MySQL</a><br/> | |
<a href='http://localhost:8081/PessoasTeste.xlsx'>Baixar versão sem MySQL</a><br/> | |
"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment