Problem with PHPExcel
I stated this several time when you originally raised thsi question... that script will generate a blank page.
$objWriter->save('CityList.xls');
writes the Excel workbook to a file called CityList.xls on the server's filesystem. It does NOT display anything to the screen... therefore the screen will be blank.
Look on the server. Find the file called CityList.xls. Open that file in MS Excel.
EDIT
Alternatively, set the appropriate headers, and save to php://output
// connection with the database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "database";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
// require the PHPExcel file
require 'Classes/PHPExcel.php';
// simple query
$query = "SELECT id FROM users ORDER by id DESC";
if ($result = mysql_query($query) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Cities');
// Loop through the result set
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myFile.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
echo 'a problem has occurred... no data retrieved from the database';
EDIT 2
Alternative:
// Loop through the result set
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
$objPHPExcel->getActiveSheet()->fromArray(array($row),NULL,'A'.$rowNumber++);
}
should also fix this error
EDIT #3
To add a heading row.
$rowNumber = 1;
$headings = array('Name','EMail','Phone');
$objPHPExcel->getActiveSheet()->fromArray(array($headings),NULL,'A'.$rowNumber);
$rowNumber++
// Loop through the result set
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
PHPExcel Warning/Error when opening document
I've had the same problem and i resolved simply putting an
exit();
justafter the $objWriter->save('php://output');
command.
Eg.
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="industrializzazione_RTW_'.$rows[0]['stagione'].'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();
Related Topics
Alternative for $_Server['Http_Referer'] PHP Variable in Msie
Issues Porting PHP/Gd Wrapper to Imagick
In PHP How to Access a ":Private" Array in an Object
How to Change the Name of an Element in Dom
Why Do I Receive "This Value Should Be of Type String" When Using a Datetime Constraint on Symfony 5
Php:Difference Between '&&' and 'And'
Change Today's Date and Time in PHP
How to Keep Leading Zeros in PHP Integer
Simplexml: Append One Tree to Another
Connect to Azure Active Directory from PHP Web Application
Simplexmlelement and Xpath, Getting Empty Array()
Php: How to Add Leading Zeros/Zero Padding to Float via Sprintf()
Upload Images Through PHP Using Unique File Names
Pdo Mssql Server - Driver Not Found
What Is Laravel Render() Method For
Set Cart Item Price from a Hidden Input Field Custom Price in Woocommerce 3