View Single Post
Old 19-09-2005, 07:18 AM   #3
Reup
10 GOSUB Abandonia
20 GOTO 10
 
Reup's Avatar

 
Join Date: Dec 2004
Location: Eindhoven, Netherlands
Posts: 1,508
Default

For anyone interested. This only works if PEAR is installed and configured on your server.

Code:
require_once 'Spreadsheet/Excel/Writer.php';

// $dataSet is any MySQL-query-result
// $title is the file-name including the complete path
// $MODE == 0 -> save as local file
// $MODE == 1 -> send HTTP headers -> open in browser! only use in NEW file, or 
// the remaining HTML will be send to the sheet as well!!!

function writeExcel( $dataSet, $title, $MODE )
{
	// Creating a workbook
	$filename = $title.'.xls';
	$pathname = '.';
	$workbook = new Spreadsheet_Excel_Writer( $pathname.$filename );
	
	// Creating formats 
	// Only bold in this case 
	$format_bold =& $workbook->addFormat();
	$format_bold->setBold();	
	
	// sending HTTP headers if $MODE is set to 1
	if( $MODE == 1 ) $workbook->send($filename);
	
	// Creating a worksheet
	$worksheet =& $workbook->addWorksheet('SU5-Aanvragen'.$datum);
	
	// Writing column headers (bold)
	$numFields = mysql_num_fields( $dataSet );
	for( $k = 0; $k < $numFields; $k++ )
	{
 *$field = mysql_fetch_field( $dataSet, $k );
 *$fieldName = $field->name;
 *
 *$worksheet->write( 0, $k, $fieldName, $format_bold );
	}
 *
	//writing data, row for row (regular)
	for( $i = 0; $i < mysql_num_rows( $dataSet ); $i++ )
	{
 *// starting form row 1, because row 0 is the header-row
 *$row = mysql_fetch_array( $dataSet );
 *$index = $i + 1;
 *for( $k = 0; $k < $numFields; $k++ )
 *{
 *	$worksheet->write( $index, $k, $row[$k], $formal_bold );
 *} *	
	}
	
	// return the filename!
	
	// Let's send the file
	$workbook->close();
	
	return $pathname.$filename;
}
Reup is offline                         Send a private message to Reup
Reply With Quote