给所有的数据我用PHPExcel
导出我的表中的数据excel文件PHPExcel不从MySQL
在我的代码我尝试从2个表导出一个excel表我的代码工作,但
的问题是我所有的数据 - 1
我的意思,如果我有5行应该是在Excel文件我只收到4
这是我的代码:
<?php
$project=$_POST['project'];
//populate the data
$row=4;
while($data=mysqli_fetch_object($query)){
$excel->getActiveSheet()->setCellvalue('A'.$row,$data->db_projectid)
->setCellvalue('B'.$row,$data->db_projectname)
->setCellvalue('C'.$row,$data->loc)
//incriment the row
$row++;
}
//make table headers
$excel->getActiveSheet()
->setCellValue('A1','Project')
->setCellValue('A3','#')
->setCellValue('B3','Project Name')
->setCellValue('C3','Location');
if(mysqli_num_rows($activitieQuery)>0){
$row=7;
while($data=mysqli_fetch_object($activitieQuery)){
$excel->getActiveSheet()
->setCellvalue('A'.$row,$data->db_id)
->setCellvalue('B'.$row,$data->db_activityname)
->setCellvalue('C'.$row,$data->db_category)
->setCellvalue('D'.$row,$data->db_priority);
//incriment the row
$row++;
}
//make table headers
$excel->getActiveSheet()
->setCellValue('A6','Activities')
->setCellValue('A7','#')
->setCellValue('B7','Activity Name')
->setCellValue('C7','Category')
->setCellValue('D7','Priority');
}
//write the result to a file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="project.xlsx"');
header('Cache-Control:max-age=0');
header('Cache-Control: max-age=1');
$file=PHPExcel_IOFactory::createWriter($excel,'Excel2007');
//output to php output instead of filename
$file->save("php://output");
exit;
?>
我如何收到我想要的所有数据而没有任何问题。
当我打印mysqli_num_rows($activitieQuery);
我有5号
,如果我打印例如$data->db_id
我收到5个id,而不是4
但是,当我导出到Excel文件,我收到4个id,而不是5我该如何解决那?
对于我的第一张表我没有任何问题我的问题是与第二张表tbl_activities只有任何一个有想法如何解决?
请修剪你的代码(删除你的程序的所有非必要的部分) 。它有两个目的:你可以在你的最后运行一个更简单的测试用例,而且它更容易阅读。例如,标题生成部分是不相关的,我们不需要知道输出文件的每一列。此外,您可能想要在Excel生成过程之外测试您的循环,因为我怀疑问题是否与Excel本身相关联(例如,在循环中只是'echo $ data-> db_id')。 – RandomSeed
@RandomSeed请检查上面的代码,并在我的循环中,我做了你所说的echo $ data-> db_id,并且我收到5个ids而不是4,但是在excel中我有4个,并且在我的问题中提到了感谢您的回复 –