如何在PHPExcel中选择特定的列顺序

时间:2014-08-23 02:28:51

标签: php mysql phpexcel

我想选择并编写特定的列名,因为我想如何做到这一点? 情况是:

$stockQuery = mysql_query("SELECT stokId,urunAdi,tlFiyat,stokMiktari,tlFiyat*stokMiktari,urunGrup,maliyetTipi FROM proje_stok where projeID =".$projeID); 

但我确实想选择

select * from proje_stok where ......

我当前的编写代码是

while($row_data = mysql_fetch_assoc($stockQuery)) {
$col = 0;
if ($col == 0) {
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order);
    $col++;
}
foreach($row_data as $key=>$value) {
   $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
   $col++;

}
$row++;
$order++;
}

而不是我想使用像

这样的代码
foreach($row_data as $key=>$value) {
   $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value['HEREWHATIWANTTOPUTCOLUMNNAME']);
   $col++;
}

有什么建议要处理吗?

问候

1 个答案:

答案 0 :(得分:1)

$col = $row = $order = 1;
if ($row_data = mysql_fetch_assoc($stockQuery))
{
    foreach ($row_data as $key => $value)
       $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $key);

    do
    {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col = 1, ++$row, $order++);
        foreach ($row_data as $value)
           $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $value);
    }
    while ($row_data = mysql_fetch_assoc($stockQuery));
}