我的任务是分别生成每个学生的excel表,所以我使用PHPExcel lib来执行任务
<?php
$host='localhost'; $user='root'; $pass=''; $DataBase='college';//define the correct values
// open the connexion to the databases server
$Link=@mysqli_connect($host,$user,$pass,$DataBase) or die('Can\'t connect !');
mysqli_set_charset($Link, 'utf8');//if not by default
//your request
if(isset($_GET['stud_id'])){
$id=$_GET['stud_id'];
$SQL='SELECT * from stud_master where stud_id=$id';
$rs=mysqli_query($Link, $SQL);//get the result (ressource)
/** Include PHPExcel */
require_once 'ec/Classes/PHPExcel.php';//change if necessary
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$F=$objPHPExcel->getActiveSheet();
$Line=1;
while($Trs=mysqli_fetch_assoc($rs)){//extract each record
$F->
setCellValue('A'.$Line, $Trs['stud_id'])->
setCellValue('B'.$Line, $Trs['course_id'])->
setCellValue('C'.$Line, $Trs['fname'])->
setCellValue('D'.$Line, $Trs['mname'])->
setCellValue('E'.$Line, $Trs['lname']);//write in the sheet
++$Line;
}
}
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
答案 0 :(得分:0)
好像,你的SQL语法有错误:
//use double quotes here, not single - otherwise $id won't be substituted
$SQL = "SELECT * from stud_master where stud_id=$id";
$rs=mysqli_query($Link, $SQL);//get the result (ressource)
但最好使用预准备语句来防止SQL注入。