Skip to content

Instantly share code, notes, and snippets.

@jlamim
Last active June 10, 2016 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlamim/dc0b8d0a8a8a93d7c9e3369f727c5673 to your computer and use it in GitHub Desktop.
Save jlamim/dc0b8d0a8a8a93d7c9e3369f727c5673 to your computer and use it in GitHub Desktop.
Formulas - Integrando a biblioteca PHPExcel com CodeIgntier
<?php
public function Formulas()
{
// Definindo o nome do arquivo (repare no uso da extensão .php, ela será substituída posteriormente por .xls ou .xlsx)
$fileName = "PHPExcelFormulas.php";
// Definindo o path de salvamento do arquivo
$saveFilePATH = "./files/".$fileName;
// Cria um novo objeto PHPExcel
$objPHPExcel = $this->phpexcel;
// Define as propriedades do documento
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setKeywords("office 2007 openxml php");
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setCategory("Test result file");
// Insere conteúdo no arquivo
$objPHPExcel->getActiveSheet()->setCellValue('A5', 'Sum:');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B2', 3);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B3', 7);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B4', 13);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B5', '=SUM(B2:B4)');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('C1', 'Range #2');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('C2', 5);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('C3', 11);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('C4', 17);
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('C5', '=SUM(C2:C4)');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('A7', 'Total of both ranges:');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B7', '=SUM(B5:C5)');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('A8', 'Minimum of both ranges:');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B8', '=MIN(B2:C4)');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('A9', 'Maximum of both ranges:');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B9', '=MAX(B2:C4)');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('A10', 'Average of both ranges:');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range #1')->setCellValue('B10', '=AVERAGE(B2:C4)');
// Renomeia a worksheet
$objPHPExcel->getActiveSheet()->setTitle('Formulas');
// Define qual a worksheet estará ativa ao abrir o arquivo
$objPHPExcel->setActiveSheetIndex(0);
// Salva no formato do Excel 2007 (xlsx)
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', $saveFilePATH));
// SAlva no formato Excel 95 (xls)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', $saveFilePATH));
echo date('H:i:s') , " Construção do arquivo concluída" , "<br/>";
echo 'Arquivo criado em ' , $saveFilePATH , "<br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment