gpt4 book ai didi

php - PHPExcel 的问题

转载 作者:行者123 更新时间:2023-12-02 22:16:54 25 4
gpt4 key购买 nike

我有一个简单的表格:

CREATE TABLE IF NOT EXISTS `users` (  `id` int(10) NOT NULL AUTO_INCREMENT,  `email` varchar(225) NOT NULL DEFAULT '',  `date` datetime DEFAULT NULL,  `status` int(1) NOT NULL DEFAULT '1',  PRIMARY KEY (`id`)) ENGINE=MyISAM;

我正在使用 PHPExcel 以 XLS 格式导出表格。我写了一个简单的 PHP 行:


$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "svn_register";

mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);

// require the PHPExcel file
require 'Classes/PHPExcel.php';

// simple query

$query = "SELECT * 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 = '';
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';

我得到一个空白页面。

最佳答案

当您最初提出这个问题时,我多次说过这一点...该脚本将生成一个空白页面。

$objWriter->save('CityList.xls');

将 Excel 工作簿写入服务器文件系统上名为 CityList.xls 的文件中。它在屏幕上显示任何内容...因此屏幕为空白。

查看服务器。找到名为 CityList.xls 的文件。在 MS Excel 中打开该文件。

编辑

或者,设置适当的 header ,然后保存到 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';

编辑2

替代方案:

// Loop through the result set
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
$objPHPExcel->getActiveSheet()->fromArray(array($row),NULL,'A'.$rowNumber++);
}

还应该修复此错误

编辑#3

添加标题行。

$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++;
}

关于php - PHPExcel 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4748067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com