PHPExcel生成完全输出

前端之家收集整理的这篇文章主要介绍了PHPExcel生成完全输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

问候,

我无法弄清楚如何正确使用PHP,特别是PHPExcel.我已经阅读了关于这个主题的多篇帖子,但我一直在圈子里跑来跑去.这是我的代码的相关部分:

$viewinv = MysqL_connect($sqlsrv,$username,$password);
if (!$viewinv) { die('Could not connect to sql server. Contact administrator.'); }
MysqL_select_db($database,$viewinv) or die('Could not connect to database. Contact administrator.');
$query = "select unit_id,config,location from inventory;";
$result = MysqL_query($query);

if ($result = MysqL_query($query) or die(MysqL_error())) {
   $objPHPExcel = new PHPExcel();
   $objPHPExcel->getActiveSheet()->setTitle('blah');

$rowNumber = 1;
$headings = array('Unit ID','Config','Location');
$objPHPExcel->getActiveSheet()->fromArray(array($headings),NULL,'A'.$rowNumber);
$rowNumber++;
while ($row = MysqL_fetch_row($result)) {
   $col = 'A';
   foreach($row as $cell) {
      $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
      $col++;
   }
   $rowNumber++;
}
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');

   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';

PHPExcel肯定是从查询输出数据,我可以看到明文的点点滴滴,但它被大量的随机字符所包围,好像我正在查看压缩或编译的数据片段的内容.

例如:

PKâh¿>G’D²Xð[Content_Types].xml­”MNÃ0…÷œ"ò%nY „švAa •(0ö¤±êØ–gúw{&i‰@ÕnbEö{ßøyìÑdÛ¸l mð¥‘×ÁX¿(ÅÛü)¿’òF¹à¡;@1_滘±Øc)j¢x/%ê…Eˆày¦ 

任何指针都将非常感激

最佳答案
通过将正确的编写器类型与正确的内容类型和文件扩展名相匹配,可能会解决该问题.

XLSX(办公室2007):

Writer      : Excel2007 (PHPExcel_Writer_Excel2007)
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

XLS(2007年之前):

Writer      : Excel5 (PHPExcel_Writer_Excel5)
Content-Type: application/vnd.ms-excel
原文链接:https://www.f2er.com/mysql/434215.html

猜你在找的MySQL相关文章