简体   繁体   English

在使用PHPexcel插件生成的可下载excel文件中未获取任何值

[英]Not getting any values in downloadable excel file generated using PHPexcel plugin

I am trying to use PHPexcel plugin to generate downloadable excel file from mysql data. 我正在尝试使用PHPexcel插件从mysql数据生成可下载的excel文件。 Unfortunately, I am only getting only the column headers and not their values in the downloaded excel file 不幸的是,我在下载的excel文件中仅得到列标题,而没有得到它们的值。

Where am I going wrong ? 我要去哪里错了? Also, I would like unicode characters to show properly. 另外,我希望unicode字符正确显示。 Following is my code of the downloadexcel.php file - 以下是我的downloadexcel.php文件的代码-

<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';

$objPHPExcel = new PHPExcel();

$data=array();

$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t 
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
 $result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result))   {
    $array=array("T Link" => $row[0],"T"=>$row[1] , "User Name" => $row[2] , 
"User Profile" => $row[3], "Time" => $row[4] , "Place" => $row[5]);
     array_push($data,$array);
  }

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');

$row=2;
foreach($data as $row->$value) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$value->t_id);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$value->t_text);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row,$value->user_name);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row,$value->description);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$row,$value->time);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$row,$value->place);
    $row++;
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>

Based on comments of Mark Baker, edited the code but still not getting any result. 根据Mark Ba​​ker的评论,对代码进行了编辑,但仍未得到任何结果。 This is my edited code - 这是我编辑的代码 -

<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';

$objPHPExcel = new PHPExcel();

$data=array();

$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t 
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
 $result = mysqli_query($con,$sql);

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');

while ($row = mysqli_fetch_array($result))
      {
$n=2;
foreach($data as $row) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$n,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$n,$row['place']);
    $n++;
   }
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>

Change this line: 更改此行:

foreach($data as $row->$value) {

to

foreach($data as $row => $value) {

"->" operator is used with objects. 对象使用“->”运算符。 The difference between the two operators is explained here: https://stackoverflow.com/a/14037376/577778 此处说明了两个运算符之间的区别: https : //stackoverflow.com/a/14037376/577778

Updated Code 更新的代码

<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';

$objPHPExcel = new PHPExcel();

$data=array();

$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t 
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
 $result = mysqli_query($con,$sql);

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');

$n=2;

while ($row = mysqli_fetch_array($result))
      {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$n,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$n,$row['place']);
    $n++;
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM