Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- require_once 'PHPExcel.php';
- clearstatcache();
- /**
- * When I uncomment SetCellAsPercentage() functionality at line 214-215 - export breaks;
- * When I uncomment preset cell-border-style at line 66-80 - export breaks again;
- */
- /** Error reporting */
- error_reporting(E_ALL);
- ini_set('display_errors', TRUE);
- ini_set('display_startup_errors', TRUE);
- date_default_timezone_set('Europe/London');
- define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
- //fill in sample data
- $stat_show_excel= 10;
- $stat_client_ids = array("Test Client");
- $stat_client_id = 0;
- $stat_year=2013;
- $main_columns = array ( 1 => array ( 'nm_abrev' => 'Jan', 'nm' => 'Jan', 'num' => 1, ), 2 => array ( 'nm_abrev' => 'Feb', 'nm' => 'Feb', 'num' => 2, ), 3 => array ( 'nm_abrev' => 'Mar', 'nm' => 'Mar', 'num' => 3, ), 4 => array ( 'nm_abrev' => 'Apr', 'nm' => 'Apr', 'num' => 4, ), 5 => array ( 'nm_abrev' => 'May', 'nm' => 'May', 'num' => 5, ), 6 => array ( 'nm_abrev' => 'Jun', 'nm' => 'Jun', 'num' => 6, ), 7 => array ( 'nm_abrev' => 'Jul', 'nm' => 'Jul', 'num' => 7, ), 8 => array ( 'nm_abrev' => 'Aug', 'nm' => 'Aug', 'num' => 8, ), 9 => array ( 'nm_abrev' => 'Sep', 'nm' => 'Sep', 'num' => 9, ), 10 => array ( 'nm_abrev' => 'Oct', 'nm' => 'Oct', 'num' => 10, ), 11 => array ( 'nm_abrev' => 'Nov', 'nm' => 'Nov', 'num' => 11, ), 12 => array ( 'nm_abrev' => 'Dec', 'nm' => 'Dec', 'num' => 12, ), );
- $g_groups = array ( 0 => array ( 'group_name' => 'group name', 'group_ord' => '0', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 1 => array ( 'group_name' => 'group name', 'group_ord' => '1', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 2 => array ( 'group_name' => 'group name', 'group_ord' => '2', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 3 => array ( 'group_name' => 'group name', 'group_ord' => '3', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 4 => array ( 'group_name' => 'group name', 'group_ord' => '4', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 5 => array ( 'group_name' => 'group name', 'group_ord' => '5', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 6 => array ( 'group_name' => 'group name', 'group_ord' => '6', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), 7 => array ( 'group_name' => 'group name', 'group_ord' => '7', 'bloc_ord' => '0', 'months' => array ( 6 => '6', 7 => '7', ), ), );
- $data_main_groups_total= array ( 'r' => array ( 0 => array ( 'points_got' => 102, 'points_from' => 143, 'sum' => 10200, 'cnt' => 143, 'total' => 71, ), 'row_avg' => array ( 'sum' => 94223.333332, 'cnt' => 1467, 'total' => 64, ), 1 => array ( 'points_got' => 205, 'points_from' => 328, 'sum' => 17300, 'cnt' => 291, 'total' => 59, ), 2 => array ( 'points_got' => 613, 'points_from' => 924, 'sum' => 13983.333332, 'cnt' => 295, 'total' => 47, ), 3 => array ( 'points_got' => 88, 'points_from' => 110, 'sum' => 8800, 'cnt' => 110, 'total' => 80, ), 4 => array ( 'points_got' => 166, 'points_from' => 258, 'sum' => 16600, 'cnt' => 258, 'total' => 64, ), 5 => array ( 'points_got' => 19, 'points_from' => 74, 'sum' => 1900, 'cnt' => 74, 'total' => 26, ), 6 => array ( 'points_got' => 98, 'points_from' => 111, 'sum' => 9800, 'cnt' => 111, 'total' => 88, ), 7 => array ( 'points_got' => 358, 'points_from' => 518, 'sum' => 15640, 'cnt' => 185, 'total' => 85, ), ), 'block_result' => array ( 0 => array ( 'sum' => 94223.333332, 'cnt' => 1467, 'total' => 64, ), ), 'c' => array ( 6 => array ( 'sum' => 84595.5555543, 'cnt' => '1320.00000000000000000000', 'total' => 64, ), 7 => array ( 'sum' => 9627.7777777, 'cnt' => '147.00000000000000000000', 'total' => 65, ), ), );
- $data_main_groups=array ( 0 => array ( 6 => 72, 7 => 64, ), 1 => array ( 6 => 58, 7 => 70, ), 2 => array ( 6 => 47, 7 => 51, ), 3 => array ( 6 => 81, 7 => 73, ), 4 => array ( 6 => 65, 7 => 59, ), 5 => array ( 6 => 21, 7 => 63, ), 6 => array ( 6 => 89, 7 => 83, ), 7 => array ( 6 => 85, 7 => 78, ), );
- $export = new Exporter();
- //fill data and make calculations
- $export->Groups($stat_show_excel, $stat_client_ids, $stat_client_id, $stat_year, $main_columns, $g_groups, $data_main_groups_total, $data_main_groups);
- //export
- $export->close();
- class Exporter {
- protected $workbook;
- protected $format;
- protected $worksheet;
- private $objPHPExcel;
- private $setCellAsPercentageCount = 0;
- function __construct() {
- $this->objPHPExcel = new PHPExcel();
- $this->objPHPExcel->getProperties()->setCreator("Client-X Office")
- ->setLastModifiedBy("Client-X Office")
- ->setTitle("Client-X Office")
- ->setSubject("Client-X Office")
- ->setDescription("This document is generated by Client-X system.")
- ->setKeywords("Client-X mystery shopper")
- ->setCategory("Client-X Office");
- $this->objPHPExcel->removeSheetByIndex(0);
- $this->SetFormats();
- }
- function setFormats(){
- $this->format['white'] = array(
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
- ),
- 'fill' => array(
- 'type' => PHPExcel_Style_Fill::FILL_SOLID,
- 'color' => array(
- 'rgb' => 'FFCCFF'
- ),
- ), //uncomment code below and export breaks
- // 'borders' => array(
- // 'inside' => array(
- // 'style' => PHPExcel_Style_Border::BORDER_THIN,
- // 'color' => array(
- // 'argb' => 'FFFF0000'
- // )
- // ),
- // 'outline' => array(
- // 'style' => PHPExcel_Style_Border::BORDER_THIN,
- // 'color' => array(
- // 'argb' => 'FFFF0000'
- // )
- // ),
- // ),
- );
- $this->format['blue_total'] = array(
- 'font' => array(
- 'bold' => true,
- 'color' => array( 'argb' => 'FFFFFFFF'),
- ),
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
- ),
- 'fill' => array(
- 'type' => PHPExcel_Style_Fill::FILL_SOLID,
- 'color' => array(
- 'rgb' => '31859B'
- ),
- ),
- 'borders' => array(
- 'outline' => array(
- 'style' => PHPExcel_Style_Border::BORDER_THIN,
- 'color' => array('argb' => 'FF000000'),
- ),
- ),
- );
- }
- function close(){ //this method
- // header('Content-Type: application/vnd.ms-excel');
- // header('Content-Disposition: attachment;filename="result.xls"');
- // header('Cache-Control: max-age=0');
- $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel5');
- // $objWriter->save('php://output');
- $objWriter->save(str_replace('.php', '.xls', __FILE__));
- echo "Php memory limit: ".ini_get('memory_limit')."<br/>"; //2048M
- echo "Php time limit: ". ini_get('max_execution_time')."<br/>"; //30
- echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL; //4.75 MB
- echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; //4.75 MB
- echo "SetCellAsPercentage called: ".$this->setCellAsPercentageCount++; //26
- // $this->workbook->close();
- }
- function AddHeader(){
- $this->SetBackground('A1:N5', '00CCFF');
- $this->SetBackground('A6:N10', '000000');
- $this->SetForeground('A6:N10', 'FFFFFF');
- $this->SetBackground('A11:N14', 'FFFFFF');
- //Add logo image
- //bmp file - 40kb
- }
- private function SetCellAsPercentage($coords){
- // $this->objPHPExcel->getActiveSheet()->getStyle($coords)
- // ->getNumberFormat()->setFormatCode('0.00%');
- $this->setCellAsPercentageCount++;
- }
- function Groups($stat_show_excel, $stat_client_ids,$stat_client_id, $stat_year, $main_columns, $g_groups, $data_main_groups_total, $data_main_groups){
- $workSheetName = '1.Sections';
- $newWorkSheet = new PHPExcel_Worksheet($this->objPHPExcel, $workSheetName);
- $this->objPHPExcel->addSheet($newWorkSheet);
- $this->objPHPExcel->setActiveSheetIndexByName($workSheetName);
- $this->AddHeader();
- $this->SetCellValue('A6', 'Клиент: '.$stat_client_ids[$stat_client_id]);
- $this->SetCellValue('A7', 'Година: '.$stat_year);
- $this->SetBackground('A15:N15', 'FFFFFF');
- $this->AlignHorizontal('A15:N15', 'center');
- $this->AlignVertical('A15:N15', 'center');
- $this->SetBorders('A15:N15', 'top', 'thin');
- $this->SetBorders('A15:N15', 'bottom', 'thin');
- $this->objPHPExcel->getActiveSheet()->getRowDimension('15')->setRowHeight(30);
- $this->SetCellValue('A15', 'Групи');
- $this->objPHPExcel->getActiveSheet()->getStyle('A15')->getFont()->setBold(true);
- $this->SetCellValue('B15', 'Общо');
- $this->objPHPExcel->getActiveSheet()->getStyle('B15')->getFont()->setBold(true);
- $i = 1;
- foreach ($main_columns as $v) {
- $i++;
- $coords = $this->ToCoords(14, $i);
- $this->SetCellValue($coords, $v['nm']);
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getFont()->setBold(true);
- // $this->objPHPExcel->getActiveSheet()->getColumnDimension($this->ToCoords(null, $i))->setAutoSize(true);
- $this->objPHPExcel->getActiveSheet()->getColumnDimension($this->ToCoords(null, $i))->setWidth(7);
- }
- $i = 15;
- foreach ($g_groups as $k => $v) {
- $coords = $this->ToCoords($i, 0);
- $this->SetCellValue($coords, $v['group_name']);
- $this->SetBorders($coords, 'top', 'thin');
- $this->SetBorders($coords, 'bottom', 'thin');
- $this->AlignVertical($coords, 'center');
- $this->SetBackground($coords, '99CCFF');
- $this->WrapCell($coords);
- $this->objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(70);
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getFont()->setBold(true);
- $coords = $this->ToCoords($i, 1);
- if (isset($data_main_groups_total['r'][$v['group_ord']]['total'])) {
- $value = $data_main_groups_total['r'][$v['group_ord']]['total']/100;
- $this->SetCellAsPercentage($coords);
- $this->SetCellValue($coords, $value);
- $this->objPHPExcel->getActiveSheet()
- ->getStyle($coords)->applyFromArray($this->format['white']);
- // $objPHPExcel->getActiveSheet()->getStyle($coords)->getNumberFormat()
- // ->setFormatCode('#,##0.00%');
- }
- $j = 2;
- foreach ($main_columns as $vv) {
- $coords = $this->ToCoords($i, $j);
- if (!empty($v['months'][$vv['num']])) {
- if (!($data_main_groups[$v['group_ord']][$vv['num']] === "N/A")) {
- $value = $data_main_groups[$v['group_ord']][$vv['num']]/100;
- $this->SetCellAsPercentage($coords);
- } else {
- $value = 'N/A';
- }
- }
- else {
- $value ='-';
- }
- $this->SetCellValue($coords, $value);
- $j++;
- }
- $frCoords = $this->ToCoords($i, 2);
- $toCoords = $this->ToCoords($i, 13);
- $concatCoords = $frCoords.':'.$toCoords; //C16:N16
- $this->objPHPExcel->getActiveSheet()
- ->getStyle($concatCoords)->applyFromArray($this->format['white']);
- $i++;
- }
- $frCoords = $this->ToCoords($i, 0);
- $toCoords = $this->ToCoords($i, 13);
- // $this->objPHPExcel->getActiveSheet()
- // ->getStyle($frCoords.':'.$toCoords)->applyFromArray($this->format['white']);
- $i++;
- if (count($g_groups)) {
- $frCoords = $this->ToCoords($i, 0);
- $toCoords = $this->ToCoords($i, 13);
- $coords = $this->ToCoords($i, 0);
- $this->SetBackground($frCoords.':'.$toCoords, 'FFFFFF');
- $this->setCellValue($coords, 'Общо');
- $j = 2;
- foreach ($main_columns as $v) {
- $coords = $this->ToCoords($i, $j);
- if (isset($data_main_groups_total['c'][$v['num']]['total'])) {
- $value = (float)$data_main_groups_total['c'][$v['num']]['total']/100;
- $this->SetCellAsPercentage($coords);
- }
- else {
- $value = '-';
- }
- $this->objPHPExcel->getActiveSheet()
- ->getStyle($frCoords.':'.$toCoords)->applyFromArray($this->format['blue_total']);
- $this->setCellValue($coords, $value);
- $j++;
- }
- }
- $this->objPHPExcel->getActiveSheet()->freezePane('C1');
- }
- //Controllers
- function AlignHorizontal($coords, $alignment){
- switch ($alignment) {
- case 'center':
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- break;
- default:
- # code...
- break;
- }
- }
- function AlignVertical($coords, $alignment){
- $align = "";
- switch ($alignment) {
- case 'center':
- $align = PHPExcel_Style_Alignment::VERTICAL_CENTER;
- break;
- default:
- return;
- break;
- }
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getAlignment()->setVertical($align);
- }
- function SetBorders($coords, $borderPosition, $style){
- $borderStyle = '';
- switch ($style) {
- case 'thin':
- $borderStyle = PHPExcel_Style_Border::BORDER_THIN;
- break;
- default:
- throw new Exception("Define a border style", 1);
- break;
- }
- switch ($borderPosition) {
- case 'top':
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getBorders()->getTop()->setBorderStyle($borderStyle);
- break;
- case 'bottom':
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getBorders()->getBottom()->setBorderStyle($borderStyle);
- break;
- default:
- throw new Exception("Define border position", 1);
- break;
- }
- }
- function SetBackground($coords, $color){
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
- ->getStartColor()->setARGB('FF'.$color);
- }
- function SetForeground($coords, $color){
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)
- ->getFont()->getColor()->setARGB('FF'.$color);
- }
- function SetCellValue($coords, $value){
- $this->objPHPExcel->getActiveSheet()
- ->setCellValue($coords, $value);
- }
- function ToCoords($row, $col){
- $coord = strtoupper(chr($col+97));
- if(!is_null($row)){
- $coord .= $row+1;
- }
- return $coord;
- }
- function WrapCell($coords){
- $this->objPHPExcel->getActiveSheet()->getStyle($coords)
- ->getAlignment()->setWrapText(true);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement