-
Notifications
You must be signed in to change notification settings - Fork 0
/
Excel.php
85 lines (75 loc) · 3.36 KB
/
Excel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
}
}