<?php
require_once('Spreadsheet/Excel/Writer.php');
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
//include ('function.inc.php');


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet('List of Ship');
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
}
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);


$columnWidth = 10;
 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));
 
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
 
$regularleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ));  
$regularcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  )); 
  
$TitleFormat =& $workbook->addFormat(array('size'=>11,
  'align'=>'left',
  'bold'=>1
  ));

$boldleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
$boldcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));    
     	

$worksheet->write(1,0,$CompanyName, $TitleFormat);
$worksheet->write(1,1,"List of Ship", $TitleFormat);
               
$worksheet->write(1,4,"Printed Date:", $boldleftFormat);
$worksheet->write(1,5,$printdate[2]."/".$printdate[1]."/".$printdate[0], $regularleftFormat);
$worksheet->write(2,4,"Time:", $boldleftFormat);		
$worksheet->write(2,5,$dt[1]." ".$dt[2], $regularleftFormat);
  

  
$RowNum    = 4;
$ColumnNum = 0;

$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
$worksheet->write($RowNum,$ColumnNum,"Ship Code", $boldleftFormat);
$ColumnNum += 1;
$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
$worksheet->write($RowNum,$ColumnNum,"Ship Name", $boldleftFormat);
$ColumnNum += 1;



$query_Ship=" Select Ship_code , Ship_Name 
				 From Ship ";

	
$result_Ship=mysql_query($query_Ship,$connection) or die(mysql_error());
$totalrecords=mysql_num_rows($result_Ship);

$RowNum    = 5;
$ColumnNum = 0;

while($row=mysql_fetch_row($result_Ship)){

	$worksheet->write($RowNum, $ColumnNum, $row[0], $regularleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $row[1], $regularleftFormat);
	$ColumnNum += 1;
	
	$RowNum += 1;
	$ColumnNum = 0;	
}

$RowNum += 1;
$ColumnNum = 0;	
	
$worksheet->write($RowNum, $ColumnNum, "Total Records: ".$totalrecords, $regularleftFormat);
$ColumnNum += 1;	

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}
$workbook->send('ListOfShip.xls');
$workbook->close();
?>