这是我创建excel文件的PHP代码。即使excel正在创建,但数据不会从数据库表返回。
<?php
$dbhost = "localhost";
$dbuser = "MY DATABASE USERNAME";
$dbpass = "MY DATABASE PASSWORD";
function dbConnect($db="") { global $dbhost, $dbuser, $dbpass;
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
or die("The site database appears to be down.");
if ($db!="" and !@mysql_select_db($db))
die("The site database is unavailable.");
return $dbcnx;
}
require_once 'excel_files/Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer();
$format_bold =& $workbook->addFormat();
$format_bold->setBold();
$worksheet =& $workbook->addWorksheet();
$worksheet->write(0, 0, "Sl.No", $format_bold);
$worksheet->write(0, 1, "Employee's Name", $format_bold);
$worksheet->write(0, 2, "Net pay for the month", $format_bold);
$worksheet->write(0, 3, "Account Number", $format_bold);
//$worksheet->write(0, 4, "comments", $format_bold);
# start by opening a query string
$fullquery="select * from payroll";
$result=mysql_db_query($dbuser,$fullquery);
$i=1;
while($row=mysql_fetch_assoc($result)){
$worksheet->write($i, 0, "$row[sl_no]");
$worksheet->write($i, 1, "$row[employee_name]");
$worksheet->write($i, 2, "$row[salary]");
$worksheet->write($i, 3, "$row[account_no]");
//$worksheet->write($i, 4, "$row[comments]");
$i++;
}
$workbook->send('test.xls');
$workbook->close();
?>