我想将phpexcel中的10autofilter.php用于我们的程序。
但是我想要一个可以在excel上打印我们数据库中数据的代码,因为它只打印第1行,而不打印我们mysql上的所有数据,请帮助我,您可以看到它仅输出1行的代码。
我认为这里存在问题,但这在浏览器上显示的php上效果很好,但是在excel中输出不是1行。
我在$ row上使用了$i++
,因为您可以看到我不知道该怎么办。
$res = mysql_query("select * from services");
$row = mysql_num_rows($res);
for($i=0; $i<$row; $i++)
{
$serviceid = mysql_result($res,$i,"serviceid");
$servicename = mysql_result($res,$i,"servicename");
$contactemail = mysql_result($res,$i,"contactemail");
$charge = mysql_result($res,$i,"charge");
$contactlastname = mysql_result($res,$i,"contactlastname");
$contactmiddlename = mysql_result($res,$i,"contactmiddlename");
$yearassistancereceived = mysql_result($res,$i,"yearassistancereceived");
$yearestablished = mysql_result($res,$i,"yearestablished");
$dataArray = array(
array(
$serviceid,
$servicename,
$contactemail,
$charge." ".$contactmiddlename." ".$contactlastname,
$yearassistancereceived,
$yearestablished
)
);
$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A2');
}
我也有错误:
致命错误。消息“传递了无效参数”的未捕获异常“ PHPExcel_WriterException”。为什么?在错误的底部,在第263行上将C:\ xampp \ htdocs \ DOSTPROJECT \ classes \ PHPExcel \ Writer \ Excel2007 \ ContentTypes.php写入。
这将执行和excel文件,但我只说了1行数据,我的意思是怎么了?
最佳答案
如果将每一行数据写入电子表格第2行,则各行将相互覆盖。您要将第一行数据写入电子表格第2行,第二行写入电子表格第3行,依此类推
您曾使用$ i ++从数据库中获取每一行,但在写入PHPExcel时未使用它,而只是在单元格A2
中写入了每一行数组
for($i=0; $i<$row; $i++)
{
$serviceid = mysql_result($res,$i,"serviceid");
$servicename = mysql_result($res,$i,"servicename");
$contactemail = mysql_result($res,$i,"contactemail");
$charge = mysql_result($res,$i,"charge");
$contactlastname = mysql_result($res,$i,"contactlastname");
$contactmiddlename = mysql_result($res,$i,"contactmiddlename");
$yearassistancereceived = mysql_result($res,$i,"yearassistancereceived");
$yearestablished = mysql_result($res,$i,"yearestablished");
$dataArray = array(
array(
$serviceid,
$servicename,
$contactemail,
$charge." ".$contactmiddlename." ".$contactlastname,
$yearassistancereceived,
$yearestablished
)
);
$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A'.($i+2));
}