-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.class.php
99 lines (86 loc) · 2.62 KB
/
excel.class.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Created by: Carlos Henrique Fagundes
* https://github.com/kiaonline
*
* A simple way to create a xls file
*
*/
class Excel{
private $data = array();
function __construct(Array ...$columns){
if(!empty($columns)) $this->setColumns($columns);
}
function getData(){
return $this->data;
}
function setData(Array $data){
$this->data = $data;
}
function setColumns(...$columns){
if(!empty($columns)){
array_unshift($this->data, $columns);
}
}
function addRow(...$data){
$this->data[] = $data;
}
function addRows(Array ...$data){
foreach($data as $item){
array_push($this->data,$item);
}
}
private function BOF() {
return pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
private function EOF() {
return pack("ss", 0x0A, 0x00);
}
private function colNumber($r, $c, $v) {
$str = pack("sssss", 0x203, 14, $r, $c, 0x0);
$str .= pack("d", $v);
return $str;
}
private function colString($r, $c, $v) {
$l = strlen($v);
$str = pack("ssssss", 0x204, 8 + $l, $r, $c, 0x0, $l);
$str .= $v;
return $str;
}
public function toJson(){
return json_encode($this->data);
}
private function processData(){
$str = $this->BOF();
foreach($this->data as $row_index => $row){
foreach($row as $col_index => $value){
$isNum = is_numeric($value);
if($isNum){
$str .= $this->colNumber($row_index, $col_index, utf8_decode($value));
}else{
$str .= $this->colString($row_index, $col_index, utf8_decode($value));
}
}
}
$str .= $this->EOF();
return $str;
}
function download($filename = null){
if(!$filename) $filename = date("Y-m-d");
$filename .= ".xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
echo $this->processData();
}
function save($filename = null,$path = "./"){
if(!$filename) $filename = date("Y-m-d");
$filename .= ".xls";
$contents = $this->processData();
return file_put_contents("{$path}{$filename}", $contents);
}
}