如何通过PHP在PHPExcel中创建一个动态的Excel创建循环


How to create a dynamic loop for Excel creation in PHPExcel through PHP

我有一个简单的PHP DB查询,生成简单的2列记录。我所要做的就是把它们传递给PHPExcel表格对象。

这是我的PHPExcel代码:
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();

$objWorksheet->fromArray(
    array(
        array('My Page Report 1', 14),
        array('My Page Report 2', 12),
        array('My Page Report 3', 7),
        array('My Page Report 4', 5),
        array('My Page Report 5', 4),
        array('My Page Report 6', 2),
        array('My Page Report 7 ', 1),
        array('My Page Report 8', 1),
        array('My Page Report 9', 1)
    )
);

创建一个完美的饼状图。

现在我有一个简单的查询,我需要将其数据传递到这个Excel中。-

$result = db_query("
    SELECT
      n.title, n.nid,
      COUNT(*) AS times
    FROM
      {node_view_count} a, {node} n where a.nid = n.nid AND a.uid = :uid
    GROUP BY
      n.title
    ORDER BY
      times desc",
    array(':uid'=>$_GET['uid'])) -> fetchAll();

    foreach($result as $r) {    
        $resultstr_pages_visited[] = $r->title.", ".$r->times;
     }

// This creates exactly this kind of line
// 'My Page Report 9', 1 and puts a comma to the end.
implode("," , $resultstr_pages_visited);

我的问题是如何通过我的循环动态地传递这种格式的数据到PHPExcel代码。

$resultstr_pages_visited的Print_r结果为:-

Array ( [0] => 
    Array 
    ( [0] => My Page Report 1 , 14 ) 
        [1] => Array ( [0] => My Page Report 2, 12 ) 
        [2] => Array ( [0] => My Page Report 3, 7 ) 
        [3] => Array ( [0] => My Page Report 4, 5 ) 
        [4] => Array ( [0] => My Page Report 5, 4 ) 
        [5] => Array ( [0] => My Page Report 6, 2 ) 
        [6] => Array ( [0] => My Page Report 7 , 1 ) 
        [7] => Array ( [0] => My Page Report 8, 1 ) 
        [8] => Array ( [0] => My Page Report 9, 1 ) 
    )
$row = 1;
foreach($result as $r) {
    $objWorksheet->fromArray(array($r->title, $r->times), null, 'A'.$row);
    $row++;
}