-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jean Christophe
authored and
Jean Christophe
committed
Mar 2, 2017
0 parents
commit d05078b
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php namespace JC; | ||
|
||
use PHPExcel; | ||
use PHPExcel_Cell; | ||
use PHPExcel_Style_Border; | ||
use PHPExcel_Style_Fill; | ||
use PHPExcel_IOFactory; | ||
|
||
class Excel | ||
{ | ||
public static function sendExcelFile($objPHPExcel, $filename, $parameters) | ||
{ | ||
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $parameters['writer']); | ||
|
||
header('Content-type: ' . $parameters['content-type']); | ||
header('Content-Disposition: attachment; filename="' . $filename . '.' . $parameters['extension'] . '"'); | ||
header('Cache-Control: max-age=0'); | ||
|
||
$objWriter->save('php://output'); | ||
} | ||
|
||
public static function getPrettyObjPHPExcelFromHashes($hashes, $metadata) | ||
{ | ||
$objPHPExcel = new PHPExcel(); | ||
$objPHPExcel->getProperties() | ||
->setCreator($metadata['creator']) | ||
->setLastModifiedBy($metadata['lastmodifiedby']) | ||
->setTitle($metadata['title']) | ||
->setSubject($metadata['subject']) | ||
->setCompany($metadata['company']) | ||
->setDescription($metadata['description']); | ||
$objPHPExcel->getActiveSheet()->setTitle($metadata['sheet_title']); | ||
|
||
$rows = Tools::getArrayFromHashesWithKeysAsHeaders($hashes); | ||
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A1'); | ||
|
||
$last_column = PHPExcel_Cell::stringFromColumnIndex(count($rows[0]) - 1); | ||
$last_row = $objPHPExcel->getActiveSheet()->getHighestRow(); | ||
|
||
for ($i = 0; $i < count($rows[0]); $i++) { | ||
$objPHPExcel->getActiveSheet() | ||
->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($i)) | ||
->setAutoSize(true); | ||
} | ||
|
||
$header_border_style = array( | ||
'borders' => array( | ||
'bottom' => array( | ||
'style' => PHPExcel_Style_Border::BORDER_MEDIUM, | ||
'color' => array( | ||
'argb' => '000000')))); | ||
$footer_border_style = array( | ||
'borders' => array( | ||
'bottom' => array( | ||
'style' => PHPExcel_Style_Border::BORDER_THIN, | ||
'color' => array( | ||
'argb' => '000000')))); | ||
$objPHPExcel->getActiveSheet() | ||
->getStyle('A1:' . $last_column . '1') | ||
->applyFromArray($header_border_style); | ||
$objPHPExcel->getActiveSheet() | ||
->getStyle('A1:' . $last_column . '1') | ||
->getFont() | ||
->setBold(true); | ||
$objPHPExcel->getActiveSheet() | ||
->getStyle('A' . $last_row . ':' . $last_column . $last_row) | ||
->applyFromArray($footer_border_style); | ||
for ($row = 2; $row <= $last_row; $row++) { | ||
if ($row % 2 != 0) { | ||
$objPHPExcel->getActiveSheet() | ||
->getStyle('A' . $row . ':' . $last_column . $row) | ||
->applyFromArray( | ||
array( | ||
'fill' => array( | ||
'type' => PHPExcel_Style_Fill::FILL_SOLID, | ||
'color' => array( | ||
'rgb' => 'f2f2f2'))) | ||
); | ||
} | ||
} | ||
$objPHPExcel->getActiveSheet()->setSelectedCell('A1'); | ||
|
||
return $objPHPExcel; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# excel | ||
|
||
A collection of useful PHP functions to prettify hashes into an Excel file. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "johnchristopher/excel", | ||
"description": "A collection of useful PHP functions to prettify hashes into an Excel file.", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "Jean-Christophe Vanhalle", | ||
"email": "[email protected]", | ||
"role": "Developer", | ||
"homepage": "http://johnchristopher.github.io/" | ||
}], | ||
"homepage": "https://github.com/johnchristopher/excel", | ||
"require": { | ||
"phpoffice/phpexcel": "1.8.1", | ||
"johnchristopher/tools": "0.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"JC\\": "" | ||
} | ||
} | ||
} |