├── init.php ├── README.md └── classes └── Spreadsheet.php /init.php: -------------------------------------------------------------------------------- 1 | 6 | - **Compatible Kohana Version(s):** 3.3.x 7 | 8 | ## Description 9 | 10 | Kohana framework helper class to make spreadsheet creation easier 11 | 12 | ## Instalation 13 | 14 | Place the module in the modules/phpexcel. 15 | 16 | Install library PHPExcel. In the application/vendor/ create a directory phpexcel. Download from official website (http://phpexcel.codeplex.com/) the latest version of the library PHPExcel, unzip and copy the entire contents of the directory classes/ in the application/vendor/. 17 | 18 | In the application/bootstrap.php add module loading 19 | 20 | Kohana::modules(array( 21 | ... 22 | 'phpexcel' => MODPATH.'phpexcel', 23 | )); 24 | 25 | ## Usage 26 | 27 | $ws = new Spreadsheet(array( 28 | 'author' => 'Kohana-PHPExcel', 29 | 'title' => 'Report', 30 | 'subject' => 'Subject', 31 | 'description' => 'Description', 32 | )); 33 | 34 | $ws->set_active_sheet(0); 35 | $as = $ws->get_active_sheet(); 36 | $as->setTitle('Report'); 37 | 38 | $as->getDefaultStyle()->getFont()->setSize(9); 39 | 40 | $as->getColumnDimension('A')->setWidth(7); 41 | $as->getColumnDimension('B')->setWidth(40); 42 | $as->getColumnDimension('C')->setWidth(12); 43 | $as->getColumnDimension('D')->setWidth(10); 44 | 45 | $sh = array( 46 | 1 => array('Day','User','Count','Price'), 47 | 2 => array(1, 'John', 5, 587), 48 | 3 => array(2, 'Den', 3, 981), 49 | 4 => array(3, 'Anny', 1, 214) 50 | ); 51 | 52 | $ws->set_data($sh, false); 53 | $ws->send(array('name'=>'report', 'format'=>'Excel5')); 54 | -------------------------------------------------------------------------------- /classes/Spreadsheet.php: -------------------------------------------------------------------------------- 1 | 'csv', 14 | 'PDF' => 'pdf', 15 | 'HTML' => 'html', 16 | 'Excel5' => 'xls', 17 | 'Excel2007' => 'xlsx', 18 | ); 19 | private $mimes = array( 20 | 'CSV' => 'text/csv', 21 | 'PDF' => 'application/pdf', 22 | 'HTML' => 'text/html', 23 | 'Excel5' => 'application/vnd.ms-excel', 24 | 'Excel2007' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 25 | ); 26 | 27 | /** 28 | * Creates the spreadsheet with given or default settings 29 | * 30 | * @param array $headers with optional parameters: title, subject, description, author 31 | * @return void 32 | */ 33 | public function __construct($headers=array()) 34 | { 35 | $headers = array_merge(array( 36 | 'title' => 'New Spreadsheet', 37 | 'subject' => 'New Spreadsheet', 38 | 'description' => 'New Spreadsheet', 39 | 'author' => 'ClubSuntory', 40 | ), $headers); 41 | 42 | $this->_spreadsheet = new PHPExcel(); 43 | // Set properties 44 | $this->_spreadsheet->getProperties() 45 | ->setCreator( $headers['author'] ) 46 | ->setTitle( $headers['title'] ) 47 | ->setSubject( $headers['subject'] ) 48 | ->setDescription( $headers['description'] ); 49 | } 50 | 51 | /** 52 | * Set active sheet index 53 | * 54 | * @param int $index Active sheet index 55 | * @return void 56 | */ 57 | public function set_active_sheet($index) 58 | { 59 | $this->_spreadsheet->setActiveSheetIndex($index); 60 | } 61 | 62 | /** 63 | * Get the currently active sheet 64 | * 65 | * @return PHPExcel_Worksheet 66 | */ 67 | public function get_active_sheet() 68 | { 69 | return $this->_spreadsheet->getActiveSheet(); 70 | } 71 | 72 | /** 73 | * Writes cells to the spreadsheet 74 | * array( 75 | * 1 => array('A1', 'B1', 'C1', 'D1', 'E1'), 76 | * 2 => array('A2', 'B2', 'C2', 'D2', 'E2'), 77 | * 3 => array('A3', 'B3', 'C3', 'D3', 'E3'), 78 | * ); 79 | * 80 | * @param array of array( [row] => array([col]=>[value]) ) ie $arr[row][col] => value 81 | * @return void 82 | */ 83 | public function set_data(array $data, $multi_sheet=false) 84 | { 85 | //Single sheet ones can just dump everything to the current sheet 86 | if ( !$multi_sheet ) 87 | { 88 | $sheet = $this->_spreadsheet->getActiveSheet(); 89 | $this->set_sheet_data($data, $sheet); 90 | } 91 | //Have to do a little more work with multi-sheet 92 | else 93 | { 94 | foreach ($data as $sheetname=>$sheetData) 95 | { 96 | $sheet = $this->_spreadsheet->createSheet(); 97 | $sheet->setTitle($sheetname); 98 | $this->set_sheet_data($sheetData, $sheet); 99 | } 100 | //Now remove the auto-created blank sheet at start of XLS 101 | $this->_spreadsheet->removeSheetByIndex(0); 102 | } 103 | } 104 | 105 | protected function set_sheet_data(array $data, PHPExcel_Worksheet $sheet) 106 | { 107 | foreach ($data as $row =>$columns) 108 | foreach ($columns as $column=>$value) 109 | $sheet->setCellValueByColumnAndRow($column, $row, $value); 110 | } 111 | 112 | /** 113 | * Writes spreadsheet to file 114 | * 115 | * @param array $settings with optional parameters: format, path, name (no extension) 116 | * @return Path to spreadsheet 117 | */ 118 | public function save($settings=array()) 119 | { 120 | $settings = array_merge(array( 121 | 'format' => 'Excel2007', 122 | 'path' => APPPATH.'assets/downloads/spreadsheets/', 123 | 'name' => 'NewSpreadsheet', 124 | ), $settings); 125 | 126 | //Generate full path 127 | $settings['fullpath'] = $settings['path'].$settings['name'].'_'.time().'.'.$this->exts[$settings['format']]; 128 | 129 | $writer = PHPExcel_IOFactory::createWriter($this->_spreadsheet, $settings['format']); 130 | 131 | if ($settings['format'] == 'CSV') 132 | { 133 | $writer->setUseBOM(true); 134 | } 135 | $writer->save($settings['fullpath']); 136 | 137 | return $settings['fullpath']; 138 | } 139 | 140 | /** 141 | * Send spreadsheet to browser 142 | * 143 | * @param array $settings with optional parameters: format, name (no extension) 144 | * @return void 145 | */ 146 | public function send($settings=array()) 147 | { 148 | $settings = array_merge(array( 149 | 'format' => 'Excel2007', 150 | 'name' => 'NewSpreadsheet', 151 | ), $settings); 152 | 153 | $writer = PHPExcel_IOFactory::createWriter($this->_spreadsheet, $settings['format']); 154 | 155 | $ext = $this->exts[$settings['format']]; 156 | $mime = $this->mimes[$settings['format']]; 157 | 158 | if ($settings['format'] == 'CSV') 159 | { 160 | $writer->setUseBOM(true); 161 | } 162 | 163 | header("Content-Type: $mime"); 164 | header('Content-Disposition: attachment;filename="'.$settings['name'].'.'.$ext.'"'); 165 | header('Cache-Control: max-age=0'); 166 | $writer->save('php://output'); 167 | 168 | exit; 169 | } 170 | 171 | } --------------------------------------------------------------------------------