PHPExcel生成一些随机字符?
问题描述:
I have the task of generating the excel sheet of each and every student separately so I used PHPExcel lib to perform the task
<?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;
我的任务是分别生成每个学生的excel表,所以我使用PHPExcel lib来执行任务 p>
&lt;?php
$ host ='localhost'; $用户= '根'; $传= ''; $ DataBase ='college'; //定义正确的值
//打开与数据库服务器的连接
$ Link = @ mysqli_connect($ host,$ user,$ pass,$ DataBase)或die('Can \ 't connect!');
mysqli_set_charset($ Link,'utf8'); //如果不是默认情况
//你的请求
if(isset($ _ GET ['stud_id'])){
$ id = $ _ GET ['stud_id'];
$ SQL ='SELECT * from stud_master where stud_id = $ id';
$ rs = mysqli_query($ Link,$ SQL); //得到结果(ressource) )
/ **包含PHPExcel * /
n require_once'ec / Classes / PHPExcel.php'; //必要时更改
//创建新的PHPExcel对象
$ objPHPExcel = new PHPExcel();
$ F = $ objPHPExcel-&gt; getActiveSheet();
$ Line = 1;
while($ Trs = mysqli_fetch_assoc($ rs)){//提取每条记录
$ F-&gt;
setCellValue(' '。$ Line,$ Trs ['stud_id']) - &gt;
setCellValue('B'。$ Line,$ Trs ['course_id']) - &gt;
setCellValue('C'。$ Line, $红素[ 'FNAME']) - &GT;
setCellValue('D'。$ Line,$ Trs ['mname']) - &gt;
setCellValue('E'。$ Line,$ Trs ['lname']); //写在工作表中
+ ++ $ Line;
}
}
//将输出重定向到客户端的Web浏览器(Excel5)
标题('Content-Type:application / vnd.ms-excel');
标题('Content-Disposition :attachment; filename =“report.xls”');
header('Cache-Control:max-age = 0');
$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel5');
$ objWriter-&gt; save('php:// output');
exit;
code> pre>
p>
div>
答
Seems, the you have an error in your SQL syntax:
//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)
But better use prepared statements to protect from SQL injection.