我使用PHPExcel将数据从我的数据库上传到excel。下载正确,只是它为我返回一个空的excel表,我似乎没有得到问题的真正原因。如果你能帮助我,我会很高兴的。在下面找到我上传的代码段。
<?php
include("connection/db.php");
$uploadedStatus = 0;
$msg="";
include 'Classes/PHPExcel/IOFactory.php';
if(isset($_POST['submit'])){
$schId=$_POST['schId'];
$program=$_POST['program'];
$objPHPExcel = new PHPExcel();
if($program=="All"){
echo $query1 = "SELECT * FROM studentrec WHERE schId='$schId'";
}
else{
$query1 = "SELECT * FROM studentrec WHERE schId='$schId' AND program='$program'";
}
$exec1 = mysqli_query($conn,$query1) or die ("Error in Query1".mysqli_error());
$serialnumber=0;
//Set header with temp array
$tmparray =array("Student ID","First Name","Last Name");
//take new main array and set header array in it.
$sheet =array($tmparray);
while ($res1 = mysqli_fetch_array($exec1))
{
$tmparray =array();
$serialnumber = $serialnumber + 1;
array_push($tmparray,$serialnumber);
$employeelogin = $res1['stdId'];
array_push($tmparray,$employeelogin);
$employeename = $res1['fst_name'];
array_push($tmparray,$employeename);
$lst_name = $res1['lst_name'];
array_push($tmparray,$lst_name);
array_push($sheet,$tmparray);
}
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="name.xlsx"');
$worksheet = $objPHPExcel->getActiveSheet();
foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
$worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}
//make first row bold
$objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
}
?>