du90093662774150 2012-07-13 10:23
浏览 40
已采纳

too long

i'm outputting data from a mysql database to an excel workbook using PHPExcel. My workbook has 3 sheets and most of it is working fine. I'm having problems with the last section of output to the third sheet. What I am trying to do is output a table with row headers and column headers the values of which are taken from a mysql table and then a figure per row/column combination also taken from the mysql table based on what that row/column header is. The row and column headers are written to the file as they should be but the inner table figures are not. When I echo the output to screen all the data is appearing and the row/ column iterations are incrementing as they should just the setCellValueByColumnAndRow appears to not be setting the values in the worksheet. The section of code that i'm having trouble with is below. Can anyone see where the problem is in my code?

$objPHPExcel->setActiveSheetIndex(2);

while($srow = mysql_fetch_assoc($query_company))
{
$newarray[] = $srow['entity'];
}
$row4 = 2;
$col4 = 1;
while($trow = mysql_fetch_row($query_ctry))
{
$country = $trow[0];

while($comp = each($newarray))
{
$company = $comp[1];
$total = mysql_query("SELECT noparts FROM totalslist WHERE country = '$country' AND entity = '$company'") or die (mysql_error());
if ($numrows = mysql_num_rows($total) == 0)
{
$totalres = 0;
}
else
{
$result3 = mysql_fetch_row($total);
$totalres = $result3[0];
}

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col4, $row4, $totalres); 
$col4++;

}
reset($newarray);
$row4++;
$col4 = 1;
}
  • 写回答

1条回答 默认 最新

  • dragon0023 2012-07-14 17:51
    关注

    **Solution! Actually I solved this problem by changing the code slightly. I was writing the row and column headers first and then adding the inner table values after. I changed it so that I added the headers and data as I incremented through the rows and columns as shown in code below. Might help someone else.

    $objPHPExcel->createSheet(2);
    $objPHPExcel->setActiveSheetIndex(2);
    $objPHPExcel->getActiveSheet()->setTitle('PivotTable');
    $query_ctry = mysql_query("SELECT DISTINCT country FROM totalslist ORDER BY country");
    $numctry = mysql_num_rows($query_ctry);
    $query_company = mysql_query("SELECT DISTINCT entity FROM totalslist ORDER BY entity");
    $numcomp = mysql_num_rows($query_company);
    
    while($srow = mysql_fetch_assoc($query_company))
    {
        $newarray[] = $srow['entity'];
    }
    $row3 = 2;
    $col3 = 1;
    while($trow = mysql_fetch_row($query_ctry))
    {
        $country = $trow[0];
    
        while($comp = each($newarray))
        {
            $company = $comp[1];
            $total = mysql_query("SELECT noparts FROM totalslist WHERE country = '$country' AND entity = '$company'");
        if ($numrows = mysql_num_rows($total) == 0)
        {
            $totalres = 0;
        }
        else
        {
            $result3 = mysql_fetch_row($total);
            $totalres = $result3[0];
        }
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row3, $country);
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col3, 1, $company);
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col3, $row3, $totalres); 
        $col3++;
    
        }
            reset($newarray);
            $row3++;
            $col3 = 1;
    }
            // Save Excel file
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。