PHP如何将查询结束导出Excel
<form action="user.php" method="POST">PHPexcel 导出 汇出
用户编号:<input type="text" name="u_num" size="12" value="<?php echo $_POST['u_num']; ?>" />
用户名:<input type="text" name="u_name" size="20" value="<?php echo $_POST['u_name']; ?>" />
<input type="submit" name="sub" value=" 查询 " />
<input type="button" value="导出Excel" />
</form>
<table>
<tr>
<td>用户编号</td>
<td>用户名</td>
</tr>
<?php
if(!empty($_POST["u_num"])){
$u_num = "where u_id='"."{$_POST['u_num']}"."'";
}
$sql = "select * from msd.dbo.user";
$query = sqlsrv_query($conn,$sql);
if( $query === false) {
die( print_r( sqlsrv_errors(), true) );
}
while($row = sqlsrv_fetch_array($query)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
}
sqlsrv_free_stmt( $query);
sqlsrv_close( $conn);
?>
</table>
$i = 2;
while($row = sqlsrv_fetch_array($query)) {
$objPHPExcel->getActiveSheet()
->setCellValue('A'.$i, convertUTF8($row[0]))
->setCellValue('B'.$i, convertUTF8($row[1]))
->setCellValue('C'.$i, convertUTF8($row[2]))
->setCellValue('D'.$i, convertUTF8($row[3]))
->setCellValue('E'.$i, convertUTF8($row[4]))
->setCellValue('F'.$i, convertUTF8($row[5]))
->setCellValue('G'.$i, convertUTF8($row[6]))
->setCellValue('H'.$i, convertUTF8($row[7]))
->setCellValue('I'.$i, convertUTF8($row[8]))
->setCellValue('J'.$i, convertUTF8($row[9]))
->setCellValue('K'.$i, convertUTF8($row[10]))
->setCellValue('L'.$i, convertUTF8($row[11]));
$i++;
}