Created
December 17, 2013 21:04
-
-
Save drzippie/8012599 to your computer and use it in GitHub Desktop.
Basic Excel Generator PHP
This file contains hidden or 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 | |
| /** | |
| * Basic Excel Generator | |
| * | |
| * Usage: | |
| $data = array( | |
| array( 'cell_1_1', 'cell_1_2'), | |
| array( 'cell_2_1', 'cell_2_2') | |
| ); | |
| $excel = new ExcelMaker( $data ) ; | |
| file_put_contents( 'output.xls', $excel->getContents()) ; | |
| */ | |
| class ExcelMaker{ | |
| private $data = array(); | |
| /** | |
| * | |
| * @param Array $data | |
| */ | |
| public function __construct( $data ) { | |
| $this->data = $data ; | |
| } | |
| /** | |
| * | |
| * @return String Contents of Excel file | |
| */ | |
| private function getContents( ) { | |
| $contents = $this->xlsBOF(); | |
| $tr1 = 0; | |
| foreach($this->data as $k => $v ) { | |
| $nn1 = 0; | |
| foreach($v as $k1 => $v1 ) { | |
| $contents .= $this->xlsWriteLabel( $tr1, $nn1, $v1 ); | |
| $nn1++; | |
| } | |
| $tr1++; | |
| } | |
| $contents .= $this->xlsEOF(); | |
| return $contents; | |
| } | |
| private function xlsBOF() { | |
| return pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); | |
| } | |
| function xlsEOF() { | |
| return pack("ss", 0x0A, 0x00); | |
| } | |
| private function xlsWriteNumber($row, $col, $value) { | |
| return pack("sssss", 0x203, 14, $row, $col, 0x0).pack("d", $value); | |
| } | |
| function xlsWriteLabel($row, $col, $value ) { | |
| $l = strlen($value); | |
| return pack("ssssss", 0x204, 8 + $l, $row, $col, 0x0, $l). $value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment