<?php

error_reporting(E_ERROR);
include '../library/Classes/PHPExcel/IOFactory.php';


##-- get data ------------------
$data = '<meta charset="utf-8">';
if (isset($_REQUEST['data'])) {
    $data = '<meta charset="utf-8">' . $_REQUEST['data'];
}
$applyStyles = 0;
if (isset($_REQUEST['applyDefaultStyles'])) {
    $applyStyles = (int) $_REQUEST['applyDefaultStyles'];
}
$outputPath = 'C:/';
if (isset($_REQUEST['path']) && !empty($_REQUEST['path'])) {
    $outputPath = $userPath;
}
##-- start -----------------------
$outputPath = 'C:/';
$inputFileType = 'HTML';
$inputFileName = 'htmlToExport.html';
$outputFileType = 'Excel2007';
$outputFileName = $outputPath . 'excelFile ' . date('Y-m-d H-i-s') . '.xlsx';
##write html
$file = fopen($inputFileName, "w");
fwrite($file, $data);
fclose($file);
##read html file as object
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
if ($applyStyles == 1) {
    #Set properties
    $objPHPExcel->getProperties()->setCreator("GT4");
    $objPHPExcel->getProperties()->setLastModifiedBy("GT4");
    $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Document");
    $objPHPExcel->getProperties()->setSubject("XLSX Report");
    $objPHPExcel->getProperties()->setDescription("XLSX report document for Office 2007");


//$phpFont = new PHPExcel_Style_Font();
//$phpFont->setBold(true);
//$phpFont->setName('Verdana');
//$phpFont->setSize(15);
//$phpColor = new PHPExcel_Style_Color();
//$phpColor->setRGB('3276b1');
//$objPHPExcel->getActiveSheet()->getDefaultStyle()->setFont($phpFont);
//$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setColor($phpColor);

    $style = array(
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );


    $objPHPExcel->getActiveSheet()->getDefaultStyle()->applyFromArray($style);
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();
    $objPHPExcel->getActiveSheet()->getStyle("A1:" . $highestColumn . $highestRow)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    foreach (range('A', 'Z') as $columnID) {
        $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
    }
//$objPHPExcel->getActiveSheet()->getStyle('B1:B' . $highestRow)->getAlignment()->setWrapText(true);

    $style_header = array(
        'font' => array(
            'bold' => true,
            'color' => array('rgb' => '1565c0'),
            'size' => 16,
            'name' => 'Verdana'
    ));
    $objPHPExcel->getActiveSheet()->getStyle("A1:" . $highestColumn . "1")->applyFromArray($style_header);
    $sheet->getStyle("A1:" . $highestColumn . "1")->applyFromArray(
            array(
                'fill' => array(
                    'type' => PHPExcel_Style_Fill::FILL_SOLID,
                    'color' => array('rgb' => 'bdbdbd')
                )
            )
    );
}
##export excel file
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);

echo('<span style="color: #4cae4c;font-size: 16px;text-align: right;padding: 5px;"><span>تم تجهيز ملف الاكسل بنجاح</span> <span>الملف موجود فى هذا المسار:</span> <span>' . $outputFileName . '</span></span>');
?>