如何使用phpexcel使用mysql数据获取列名?

时间:2015-09-17 05:29:53

标签: php mysql

我使用phpexcel库将mysql数据导出到excel文件。代码工作正常。但是创建的excel文件没有列名。我也想要取它们。它有内置的方法吗?我需要做什么调整来制作我的代码?这是我目前的工作代码:

<?php

require_once 'PHPExcel/classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);

$rowcount = 1;

while($row = mysqli_fetch_array($rs))
{
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['id']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['title']); 
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['date']);
    $rowcount++; 
} 

$filename = "backup-" . date("h.i.s.d-m-Y") . ".xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=" . $filename);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

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

?>

3 个答案:

答案 0 :(得分:4)

使用mysqli_fetch_fields获取字段名称。

我已经编辑了代码并在代码中添加了列名。它可能会对你有帮助。

<?php

require_once 'PHPExcel/classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);

$rowcount = 1;

$fieldinfo=mysqli_fetch_fields($rs);

foreach ($fieldinfo as $val)
{
   $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $val->id);
   $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $val->title); 
   $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $val->date);
}

$rowcount = 2;

while($row = mysqli_fetch_array($rs))
{
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['title']); 
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['date']);
$rowcount++; 
} 

$filename = "backup-" . date("h.i.s.d-m-Y") . ".xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=" . $filename);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

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

?>

答案 1 :(得分:1)

如果我是对的,你正在寻找类似下面的内容

插入标题行

   $f=0;
    $fChar='A';
    while($f<mysql_num_fields($rs)){
            $objPHPExcel->getActiveSheet()->setCellValue($fChar.'1', mysql_fetch_field($rs,$f++)->name);
            $objPHPExcel->getActiveSheet()->getStyle($fChar.'1')->applyFromArray(
            array(
                'fill' => array(
                            'type' => PHPExcel_Style_Fill::FILL_SOLID,
                            'color' => array('rgb'=>'E1E0F7'),
                            ),
                'font' => array(
                    'bold' => true,
                    )
                )   
            );


        $fChar++;
    }

现在插入值

$fC=2;
while($row=mysql_fetch_array($rs,MYSQL_BOTH)){
$fChar='A';
$f=0;
    while($f<mysql_num_fields($rs)){

        $objPHPExcel->getActiveSheet()->setCellValue($fChar++.''.$fC, $row[$f++]);
    }

$fC++;  
}

答案 2 :(得分:1)

您需要更改类似下面的代码以显示列名称和数据

//this is for showing the column name
$q = mysql_query("SHOW COLUMNS FROM table_name");
if (mysql_num_rows($q) > 0) {
    while ($row_q = mysql_fetch_assoc($q)) {
        $col='A';
        $objPHPExcel->getActiveSheet()->SetCellValue($col.$rowcount, $row_q['Field']);
        $col++;
    }
    $rowcount++;
}

//this is for showing coulmn data based in results
while($row_data1 = mysql_fetch_assoc($rs)){
    $col=0;
    foreach($row_data1 as $key=>$value){
        $objPHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($col, $rowcount, $value);
        $col++;
    }
    $rowcount++;
}