Example #1
0
 public static function saveToExcel($data = false)
 {
     if ($data) {
         include "{$_SERVER['DOCUMENT_ROOT']}/admin/PHPExcel/Classes/PHPExcel.php";
         $colKeys = false;
         $titles = false;
         $file = "table.{$_SESSION['auth']['login']}.xlsx";
         $template = "{$_SERVER['DOCUMENT_ROOT']}/lib/class.table.php/empty.xlsx";
         $xls = PHPExcel_IOFactory::load($template);
         self::$sheet = $xls->getActiveSheet();
         //***************************************
         self::$sheet->setSelectedCellByColumnAndRow(0, 1);
         //			self::$sheet->setCellValueByColumnAndRow(8, 2, "VALUE");
         //			self::$sheet->mergeCellsByColumnAndRow(8, 2, 8, 3);
         //			pre(self::$sheet->getMergeCells());
         if ($data['tableInfo']) {
             //Following work will be happening with global variable $info
             self::$info = $data['tableInfo'];
             unset($data['tableInfo']);
             //Collecting information about columns(titles and column keys)
             if (self::$info['excelCols']) {
                 $colKeys = array_keys(self::$info['excelCols']);
                 $titles = array_values(array_filter(self::$info['excelCols']));
                 unset(self::$info['excelCols']);
             } elseif (self::$info['cols']) {
                 $colKeys = array_keys(self::$info['cols']);
                 $titles = array_values(array_filter(self::$info['cols']));
                 unset(self::$info['cols']);
             }
         }
         //Height of each row will be calculated automatically by the default
         if (!isset(self::$info['rowspan'])) {
             self::$info['rowspan'] = true;
         }
         self::excelGetActiveCellCoord();
         if (is_array($titles) && sizeof($titles)) {
             $titles['tableInfo']['class'] = "head";
             self::excelWriteRow($titles, false);
         }
         foreach ($data as $item) {
             self::excelWriteRow($item, $colKeys);
         }
         //***************************************
         $objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
         $objWriter->save("{$_SERVER['DOCUMENT_ROOT']}/tmp/{$file}");
         self::$info = null;
         self::$sheet = null;
         self::$acc = null;
         return $file;
     }
 }