Maison > Article > développement back-end > PHP如何将查询结束导出Excel
PHPexcel 导出 汇出
<form action="user.php" method="POST"> 用户编号:<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>
用phpexcel
http://www.jz123.cn/text/2319267.html
fputcsv()
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment; filename="aaa.xls");
用phpexcel
http://www.jz123.cn/text/2319267.html
+1
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment; filename="aaa.xls");
+1
其实是excel的功能强大..
$str="A1\tA2\tA3\n";$str.="B1\tB2\tB3\n";你把这些字符写入到文件中,后缀名改为.xls,excel直接就能读..
其实是excel的功能强大..
$str="A1\tA2\tA3\n";$str.="B1\tB2\tB3\n";你把这些字符写入到文件中,后缀名改为.xls,excel直接就能读..
其实是excel的功能强大..
$str="A1\tA2\tA3\n";$str.="B1\tB2\tB3\n";你把这些字符写入到文件中,后缀名改为.xls,excel直接就能读..
$query = sqlsrv_query($conn,$sql);if( $query === false) { die( print_r( sqlsrv_errors(), true) );}while($row = sqlsrv_fetch_array($query)) { echo $objPHPExcel->getActiveSheet() ->setCellValue('A2', convertUTF8($row[0])) ->setCellValue('B2', convertUTF8($row[1])) ->setCellValue('C2', convertUTF8($row[2])) ->setCellValue('D2', convertUTF8($row[3])) ->setCellValue('E2', convertUTF8($row[4])) ->setCellValue('F2', convertUTF8($row[5])) ->setCellValue('G2', convertUTF8($row[6])) ->setCellValue('H2', convertUTF8($row[7])) ->setCellValue('I2', convertUTF8($row[8])) ->setCellValue('J2', convertUTF8($row[9])) ->setCellValue('K2', convertUTF8($row[10])) ->setCellValue('L2', convertUTF8($row[11]));}请问这段代码有问题吗?为什么在执行时报以下错误:
Catchable fatal error: Object of class PHPExcel_Worksheet could not be converted to string in D:\MarsdenWeb\Report\output_002.php on line 19
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
->setCellValue(' A2', convertUTF8($row[0]))
->setCellValue(' B2', convertUTF8($row[1]))
->setCellValue(' C2', convertUTF8($row[2]))
->setCellValue('D2', convertUTF8($row[3]))
->setCellValue('E2', convertUTF8($row[4]))
->setCellValue('F2', convertUTF8($row[5]))
->setCellValue('G2', convertUTF8($row[6]))
->setCellValue('H2', convertUTF8($row[7]))
->setCellValue('I2', convertUTF8($row[8]))
->setCellValue('J2', convertUTF8($row[9]))
->setCellValue('K2', convertUTF8($row[10]))
->setCellValue('L2', convertUTF8($row[11]));
标红的部分循环中没有更改,所以会覆盖之前单元格的值。
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
->setCellValue(' A2', convertUTF8($row[0]))
->setCellValue(' B2', convertUTF8($row[1]))
->setCellValue(' C2', convertUTF8($row[2]))
->setCellValue('D2', convertUTF8($row[3]))
->setCellValue('E2', convertUTF8($row[4]))
->setCellValue('F2', convertUTF8($row[5]))
->setCellValue('G2', convertUTF8($row[6]))
->setCellValue('H2', convertUTF8($row[7]))
->setCellValue('I2', convertUTF8($row[8]))
->setCellValue('J2', convertUTF8($row[9]))
->setCellValue('K2', convertUTF8($row[10]))
->setCellValue('L2', convertUTF8($row[11]));
标红的部分循环中没有更改,所以会覆盖之前单元格的值。
不太明白你的意思,那我应该怎么改呢?
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
->setCellValue(' A2', convertUTF8($row[0]))
->setCellValue(' B2', convertUTF8($row[1]))
->setCellValue(' C2', convertUTF8($row[2]))
->setCellValue('D2', convertUTF8($row[3]))
->setCellValue('E2', convertUTF8($row[4]))
->setCellValue('F2', convertUTF8($row[5]))
->setCellValue('G2', convertUTF8($row[6]))
->setCellValue('H2', convertUTF8($row[7]))
->setCellValue('I2', convertUTF8($row[8]))
->setCellValue('J2', convertUTF8($row[9]))
->setCellValue('K2', convertUTF8($row[10]))
->setCellValue('L2', convertUTF8($row[11]));
标红的部分循环中没有更改,所以会覆盖之前单元格的值。
不太明白你的意思,那我应该怎么改呢?
$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++;}
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
->setCellValue(' A2', convertUTF8($row[0]))
->setCellValue(' B2', convertUTF8($row[1]))
->setCellValue(' C2', convertUTF8($row[2]))
->setCellValue('D2', convertUTF8($row[3]))
->setCellValue('E2', convertUTF8($row[4]))
->setCellValue('F2', convertUTF8($row[5]))
->setCellValue('G2', convertUTF8($row[6]))
->setCellValue('H2', convertUTF8($row[7]))
->setCellValue('I2', convertUTF8($row[8]))
->setCellValue('J2', convertUTF8($row[9]))
->setCellValue('K2', convertUTF8($row[10]))
->setCellValue('L2', convertUTF8($row[11]));
标红的部分循环中没有更改,所以会覆盖之前单元格的值。
不太明白你的意思,那我应该怎么改呢?
$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++;}
$objPHPExcel>getActiveSheet()
->setCellValue()操作的结果是返回 PHPExcel_Worksheet 对象,把前面的echo去掉吧
去掉echo后,是不报错了,但while循环好像就失效了,导出的结果就只有一条数据。
->setCellValue(' A2', convertUTF8($row[0]))
->setCellValue(' B2', convertUTF8($row[1]))
->setCellValue(' C2', convertUTF8($row[2]))
->setCellValue('D2', convertUTF8($row[3]))
->setCellValue('E2', convertUTF8($row[4]))
->setCellValue('F2', convertUTF8($row[5]))
->setCellValue('G2', convertUTF8($row[6]))
->setCellValue('H2', convertUTF8($row[7]))
->setCellValue('I2', convertUTF8($row[8]))
->setCellValue('J2', convertUTF8($row[9]))
->setCellValue('K2', convertUTF8($row[10]))
->setCellValue('L2', convertUTF8($row[11]));
标红的部分循环中没有更改,所以会覆盖之前单元格的值。
不太明白你的意思,那我应该怎么改呢?
$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++;}
其实是excel的功能强大..
$str="A1\tA2\tA3\n";$str.="B1\tB2\tB3\n";你把这些字符写入到文件中,后缀名改为.xls,excel直接就能读..