phpexcel打印mysql数据excel(在数据库上只打印1行数据到excel)

时间:2014-08-13 01:28:42

标签: php mysql phpexcel

我想使用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');
}

我也有错误:

  

致命错误。未捕获的异常&#39; PHPExcel_WriterException&#39;消息&#39;传递的参数无效&#39;。为什么?在错误的底部写入C:\ xampp \ htdocs \ DOSTPROJECT \ classes \ PHPExcel \ Writer \ Excel2007 \ ContentTypes.php第263行。

这执行和excel文件,但我只说了1行数据,我的意思是什么错了?

1 个答案:

答案 0 :(得分: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));
}