Location: PHPKode > projects > OMR-AI > omr/getexcel.php
<?php
	include_once "Spreadsheet/Excel/Writer.php";
	include('require/include_fns.php');
	$linkId= db_connect();
	$table_name = "result";

//	ini_set("memory_limit","80M");//due to image processing we have to increase the memory limit
	
function get_fields($table_name){
	$query = "SHOW COLUMNS FROM `".$table_name."`";
	$result  = mysql_query($query) or die("Query: <b>".$query."</b> Failed. Reason: ".mysql_error() );
	$num_fields = mysql_num_rows($result);
	
	for($i =0;$i<$num_fields; $i++){
		$row = mysql_fetch_assoc($result);
		$fields[$i] = $row['Field'];
	
	}
	return $fields;
}		
	
function get_data($table_name, $fields){
	
	$query = "select * from `result`";
	$result  = mysql_query($query) or die("Query: <b>".$query."</b> Failed. Reason: ".mysql_error() );
	$data_rows = mysql_num_rows($result);
	
	$num_fields = sizeof($fields);
	
	for($i=0; $i<$data_rows; $i++){
		$rows = mysql_fetch_assoc($result);
		for($j=0;$j<$num_fields; $j++){
			$data[$i][$j] = $rows[ $fields[$j] ];	
		}
	}
	return $data;
}
	
	$fields = get_fields($table_name);
	$num_fields = sizeof($fields);
	
	$data = get_data($table_name, $fields);
	$data_rows = sizeof($data);
	
	$xls =& new Spreadsheet_Excel_Writer();
	$xls->send("result.xls");
	$format =& $xls->addFormat();
	$format->setBold();
	$format->setColor("blue");
	
	$sheet=& $xls->addWorksheet('Result');
	
		$format2 =& $xls->addFormat();
		$format2->setNumFormat('0000');
			
		
		
		for($i =0; $i<$num_fields; $i++){
			
			$sheet->write(0, $i, $fields[$i], 0);
			
			for($j = 1; $j <= $data_rows; $j++){
				if($i ==1){
					$sheet->write($j, $i, $data[$j-1][$i], $format2 );
				}
				else
					$sheet->write($j, $i, $data[$j-1][$i], $format );
			}
		}
	
//	$sheet->write(0, 1, 2, 0);
//	$sheet->writeString(1, 0, "XAMPP:", 0);
//	$sheet->writeString(1, 1, $_POST['value'], $format);
		
	
	$xls->close();
	//exit;
?>
Return current item: OMR-AI