2013-03-30 119 views
1

我正在使用PHPEXCEL類。PHPExcel用while循環替換mysql結果

所以即時通訊從MySQL數據庫中的行,並打印這些行在我的Excel文件,這是正常的。其結果是這樣的:

enter image description here

所以,以前的圖像中的數字順序實際上是從usuarioxl表中的id列。

我想替換這個,因爲de「id」不斷變化。我想在計算'id'的最後一個位置,然後做一些while循環,產生一個數字順序1,2,3,4,5,6,7,8 ...我怎麼能做到這一點(取代id列與我自己的while循環與此代碼)。

$query = "SELECT id,text,util FROM usuarioxl"; 
$headings = array('#', 'Estos son los asuntos pendientes hasta la fecha','fecha de ingreso'); 
if ($result = mysql_query($query) or die(mysql_error())) { 
    // Create a new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getActiveSheet()->setTitle('Asuntos Pendientes'); 
    $rowNumber = 2; 
    $rowNumber2 = 2; 
    $col = 'A'; 
    foreach($headings as $heading) { 
    $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading); 
    $col++; 
    } 
    // Loop through the result set 
    $rowNumber = 3; 
    while ($row = mysql_fetch_row($result)) { 
    $col = 'A'; 
    foreach($row as $cell) { 
    $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
    $col++; 
    } 
    $rowNumber++; 
    $rowNumber2++; 
    } 
$styleArray = array(
     'borders' => array(
      'allborders' => array(
        'style' => PHPExcel_Style_Border::BORDER_THIN, 
        'color' => array('argb' => 'FFB0c4dE'), 
      ), 


     ), 
); 

$styleArray2 = array(
      'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID, 
        'color' => array('argb' => 'FFB0c4dE'), 
      ), 

); 

$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->applyFromArray($styleArray); 
$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($styleArray2); 
$objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(80); 
$objDrawing = new PHPExcel_Worksheet_Drawing(); 
$objDrawing->setName('Logo'); 
$objDrawing->setDescription('Logo'); 
$objDrawing->setPath('./images/headerexcel2.png'); 
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 
$objDrawing->setCoordinates('A1'); 
$objDrawing->setOffsetX(30); 

    // Freeze pane so that the heading line won't scroll 

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5); 
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50); 
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30); 
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(50); 
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(15); 
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&16'); 
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial'); 
$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 
$objPHPExcel->getActiveSheet()->freezePane('A2'); 

    // Save as an Excel BIFF (xls) file 



    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
    header('Content-Type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment;filename="asuntos-pendientes.xls"'); 
    header('Cache-Control: max-age=0'); 


    $objWriter->save('php://output'); 

    exit(); 
+0

那麼究竟你要替換的ID是什麼? –

回答

1

我會想:

$query = "SELECT text, util FROM usuarioxl"; 

... 

$rowNumber = 3; 
$newId = 0; 

while ($row = mysql_fetch_row($result)) { 
    $col = 'B'; 

    $objPHPExcel->getActiveSheet()->setCellValue("A$rowNumber", $newId++); 

    foreach($row as $cell) { 
     $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber, $cell); 
     $col++; 
    } 

    $rowNumber++; 
    $rowNumber2++; 
} 
+0

!非常感謝@Jared,非常好! – JuanFernandoz