将数据从mysql插入到phpexcel

时间:2017-04-25 10:24:48

标签: php mysql

我使用PHPEXCEL类导出数据,使用从mysql到excel的柱形图。

如果我从phpexcel中的示例中读取使用数组

$sheet = array(

  array(
    '',
    '2010',
    '2011',
    '2012',
  ),
  array(
    'Q1',
    '12',
    '15',
    '21',
  ),
  array(
    'Q2',
    '56',
    '73',
    '86',
  ),
  array(
    'Q3',
    '52',
    '43',
    '66',
  ),
  array(
    'Q4',
    '52',
    '33',
    '46',
  )
);




foreach($sheet as $row => $columns) {
    foreach($columns as $column => $data) {
        $objWorksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
    }
}
?>

我有mysql代码:

$gdg = mysql_query("SELECT a.building_id, b.building_name FROM ticket_open a JOIN m_building b ON a.building_id = b.building_id GROUP BY b.building_id ORDER BY b.building_name ASC");
     while($hsldgd = mysql_fetch_array($gdg))                    
     {

        $gdgid = $hsldgd['building_id']; 
        $gdgname = $hsldgd['building_name'];

            $comp = mysql_query("SELECT ifnull(count(ticket_no),0) as jcomp FROM ticket_open WHERE STATUS = 'completed' AND 
                                 building_id = '$gdgid' ");
            $hslcomp = mysql_fetch_array($comp);    

            $ncomp = mysql_query("SELECT ifnull(count(ticket_no),0) as jncomp FROM ticket_open WHERE STATUS != 'completed' AND 
                                 building_id = '$gdgid'");
            $hslncomp = mysql_fetch_array($ncomp);  


        $jmlnot = $hslncomp['jncomp'];
        $jmlcomp = $hslcomp['jcomp'];
        $jmltick = $hslncomp['jncomp'] + $hslcomp['jcomp'] ;


        $sheet = array (
                array (

                $gdgname,
                $jmlnot,
                $jmlcomp,
                $jmltick,
                ),
            );


foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
    $objWorksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}

此代码仅读取最后一个数据,而不是所有数据

请帮帮我:)。

谢谢,

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

$sheet = array();
$gdg = mysql_query("SELECT a.building_id, b.building_name FROM ticket_open a JOIN m_building b ON a.building_id = b.building_id GROUP BY b.building_id ORDER BY b.building_name ASC");
     while($hsldgd = mysql_fetch_array($gdg))                    
     {

        $gdgid = $hsldgd['building_id']; 
        $gdgname = $hsldgd['building_name'];

            $comp = mysql_query("SELECT ifnull(count(ticket_no),0) as jcomp FROM ticket_open WHERE STATUS = 'completed' AND 
                                 building_id = '$gdgid' ");
            $hslcomp = mysql_fetch_array($comp);    

            $ncomp = mysql_query("SELECT ifnull(count(ticket_no),0) as jncomp FROM ticket_open WHERE STATUS != 'completed' AND 
                                 building_id = '$gdgid'");
            $hslncomp = mysql_fetch_array($ncomp);  


        $jmlnot = $hslncomp['jncomp'];
        $jmlcomp = $hslcomp['jcomp'];
        $jmltick = $hslncomp['jncomp'] + $hslcomp['jcomp'] ;


        $sheet[] = array (
                $gdgname,
                $jmlnot,
                $jmlcomp,
                $jmltick
                );

foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
    $objWorksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}

如果不起作用,请var_dump $sheet变量。

如前所述,你可能想修复你的mysql查询。