普通网友 2014-07-03 00:37
浏览 38

PHPExcel动态单元格生成迭代

Im about the create script that exports data from mysql database and save it to excel file(multisheet). I need to have the cells dinamically genereted. The script does the headings correctly, but the data set is empty. I checked the data is there when i "echo" the $value variable. so it should have work.

Any help would appriciated. Thanks a lot.

// Add new sheet
$objWorkSheet = $objPHPExcel->createSheet($i); //Setting index when creating

//Write heading cells
$c=0;
foreach ($res['name'] as $name) {        
  $objWorkSheet->setCellValueByColumnAndRow($c, 1, $name);
  $c++;
}    
//Write data cells            
 $rowcount = 2; // 2-to skip the header
 while($celldata = mysql_fetch_assoc($qry)) {
    $col=0;            
 foreach($celldata as $key=>$value){
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$rowcount,$value);            
        $col++;
        }            
$rowcount++;
$col=0;
}

// Rename sheet
$objWorkSheet->setTitle("$title");
  • 写回答

1条回答 默认 最新

  • dongxiang7276 2014-07-03 00:46
    关注

    Try changing

    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$rowcount,$value);
    

    to

    $objWorkSheet->setCellValueByColumnAndRow($col,$rowcount,$value);
    

    because the current worksheet may not be the active sheet, so you could be writing to the wrong worksheet

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测