我正在尝试使用PHPexcel插件从mysql数据生成可下载的excel文件。不幸的是,我只在下载的excel文件中只获取列标题而不是它们的值
我哪里错了?另外,我希望unicode字符正确显示。 以下是我的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');
?>
根据Mark Baker的评论,编辑了代码,但仍然没有得到任何结果。这是我已编辑的代码 -
<?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');
?>
答案 0 :(得分:0)
更改此行:
foreach($data as $row->$value) {
到
foreach($data as $row => $value) {
&#34; - &GT;&#34;运算符与对象一起使用。这里解释了两个运算符之间的差异:https://stackoverflow.com/a/14037376/577778
更新代码
<?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');
?>