【问题标题】:Problem with PHPExcelPHPExcel 的问题
【发布时间】:2011-01-20 14:05:53
【问题描述】:

我有一张简单的桌子:

如果不存在“用户”,则创建表( `id` int(10) NOT NULL AUTO_INCREMENT, `email` varchar(225) NOT NULL DEFAULT '', `date` 日期时间 DEFAULT NULL, `status` int(1) NOT NULL DEFAULT '1', 主键(`id`) ) 引擎=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';

我得到一个空白页面。

【问题讨论】:

  • 循环退出后 $rownumber 是多少?查询中是否有任何数据?
  • 我不知道你从哪里得到你的专栏。 $col = '';

标签: php xls phpexcel


【解决方案1】:

当你最初提出这个问题时,我已经说过好几次了……那个脚本会生成一个空白页。

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

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

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

编辑

或者,设置适当的标题,并保存到 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++;
}

【讨论】:

  • 谢谢。很奇怪我得到一个错误:Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\app\Classes\PHPExcel\Worksheet.php on line 2103
  • 好的!现在我们正在取得进展。 Invalid 参数是 fromArray() 方法中的真正错误。作为使用 fromArray() 的替代方法,请在上面的示例中使用重新编辑的 while 循环版本。
  • OMG` 致命错误:在 C:\xampp\htdocs\2011\register\web\Classes\PHPExcel\Cell.php:501 Stack跟踪:#0 C:\xampp\htdocs\2011\register\web\Classes\PHPExcel\Worksheet.php(959): PHPExcel_Cell::coordinateFromString('AARRAY') #1 C:\xampp\htdocs\2011\register\ web\Classes\PHPExcel\Worksheet.php(860): PHPExcel_Worksheet->getCell('AArray') #2 C:\xampp\htdocs\2011\register\web\xls.php(31): PHPExcel_Worksheet->setCellValue(' AArray', '115') #3 {main} 在 C:\xampp\htdocs\2011\register\web\Classes\PHPExcel\Cell.php 第 501 行抛出`
  • 对这个可怕的消息感到抱歉
  • 我的错:$row 应该是 $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 行中的 $rowNumber;
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 2012-04-15
  • 2013-09-16
  • 1970-01-01
  • 2014-12-03
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多