<?php



session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysqli_query($conn,"select * from admin where username='$user'");

$gtp1=mysqli_fetch_assoc($gtp);







require_once 'PHPExcel.php';







require_once 'PHPExcel/IOFactory.php';







// Create new PHPExcel object





$objPHPExcel = new PHPExcel();





// Create a first sheet, representing sales data





//$objPHPExcel->setActiveSheetIndex(0);





$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');







// Rename sheet







//$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');







// Create a new worksheet, after the default sheet







$objPHPExcel->createSheet();







// Add some data to the second sheet, resembling some different data types







$objPHPExcel->setActiveSheetIndex(1);







$styleArray = array(





'font'  => array(





    'bold'  => true,





    'color' => array('rgb' => 'FF0000'),





));







$stylearr = array(





'font'  => array(





    'bold'  => true,





    'color' => array('rgb' => 'BD3A00'),





));







$stylear = array(





'font'  => array(





    'bold'  => true,





	'color' => array('rgb' => '00A404'),





));







$style = array(





'font'  => array(





    'bold'  => true,	





));







$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);





$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);





$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);





$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);





$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);





$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);




$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);




$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);




$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);







$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Premium Customers');



$objPHPExcel->getActiveSheet()->getStyle("C1")->applyFromArray($styleArray);



$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->applyFromArray(array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,));







$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Name')->getStyle("A3")->applyFromArray($style);





$objPHPExcel->getActiveSheet()->setCellValue('B3', 'Address')->getStyle("B3")->applyFromArray($style);





$objPHPExcel->getActiveSheet()->setCellValue('C3', 'Mobile')->getStyle("C3")->applyFromArray($style);





$objPHPExcel->getActiveSheet()->setCellValue('D3', 'State')->getStyle("D3")->applyFromArray($style);





$objPHPExcel->getActiveSheet()->setCellValue('E3', 'District')->getStyle("E3")->applyFromArray($style);





$objPHPExcel->getActiveSheet()->setCellValue('F3', 'Taluk')->getStyle("F3")->applyFromArray($style);




$objPHPExcel->getActiveSheet()->setCellValue('G3', 'Crop')->getStyle("G3")->applyFromArray($style);




$objPHPExcel->getActiveSheet()->setCellValue('H3', 'Product')->getStyle("H3")->applyFromArray($style);




$objPHPExcel->getActiveSheet()->setCellValue('I3', 'How Much Years')->getStyle("I3")->applyFromArray($style);







$q=5;







$sel=mysqli_query($conn,"select * from admin where username='$user'");

$nmrw=mysqli_num_rows($sel);

$sel1=mysqli_fetch_assoc($sel);





if($nmrw>0)

{	

	$get="select * from crm_customer where premium!='' order by name asc";

}

else

{

	$empd=mysqli_query($conn,"select * from employee where username='$user'");

	$empd1=mysqli_fetch_assoc($empd);



	$usr=$empd1['ID'];



	$get="select * from crm_customer where executive='$usr' and premium!='' order by name asc";

}





$getsite4=mysqli_query($conn,$get);



$r=mysqli_num_rows($getsite4);



while($getsite5=mysqli_fetch_assoc($getsite4))



{





$a='A'.$q;



$b='B'.$q;



$c='C'.$q;



$d='D'.$q;



$e='E'.$q;



$f='F'.$q;



$g='G'.$q;



$h='H'.$q;



$i='I'.$q;





$dis=mysqli_query($conn,"select * from districts where ID='$getsite5[district]'");

$dis1=mysqli_fetch_assoc($dis);



$ste=mysqli_query($conn,"select * from statecode where ID='$getsite5[state]'");

$ste1=mysqli_fetch_assoc($ste);



$tlk=mysqli_query($conn,"select * from taluk where ID='$getsite5[taluk]'");

$tlk1=mysqli_fetch_assoc($tlk);



$prd=mysqli_query($conn,"select * from product where ID='$getsite5[product]'");
$prd1=mysqli_fetch_assoc($prd);



$sno=$sno+1;





$objPHPExcel->getActiveSheet()->setCellValue($a, $getsite5['name']);







$objPHPExcel->getActiveSheet()->setCellValue($b, $getsite5['address']);







$objPHPExcel->getActiveSheet()->setCellValue($c, $getsite5['mobile']);







$objPHPExcel->getActiveSheet()->setCellValue($d, $ste1['state']);







$objPHPExcel->getActiveSheet()->setCellValue($e, $dis1['district']);







$objPHPExcel->getActiveSheet()->setCellValue($f, $tlk1['taluk']);






$objPHPExcel->getActiveSheet()->setCellValue($g, $getsite5['cultivatingcrop']);






$objPHPExcel->getActiveSheet()->setCellValue($h, $prd1['product']);






$objPHPExcel->getActiveSheet()->setCellValue($i, $getsite5['howmuchyears']);







$q=$q+1;





}







// Rename 2nd sheet







$objPHPExcel->getActiveSheet()->setTitle('Premium_Customers');







// Redirect output to a client’s web browser (Excel5)





header('Content-Type: application/vnd.ms-excel');





header('Content-Disposition: attachment;filename="premiumcustomer.xls"');





header('Cache-Control: max-age=0');





$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');





$objWriter->save('php://output');





}






?>