PHPExcel附加文本添加到列

时间:2013-11-22 08:30:08

标签: php phpexcel

我有PHPExcel插件来生成excel哪些数据来自数据库,如果我想在循环期间将其他文本添加到列A中,以及B列是否保留来自DB的未触动数据?

例如,DB中的A列是

alex

andy

jennifer

当输出为excel时,我想为后面的每个名称添加@domain.com,这将成为

alex@domain.com

andy@domain.com

jennifer@domain.com

代码:

$query = "SELECT mail_name, account_id FROM email ORDER BY mail_name ASC";
$headings = array('Email', 'Id');

if ($result = mysql_query($query) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('emailList');


$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
   $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
   $col++;
}

// Loop through the result set
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
    $col = 'A';
    foreach($row as $cell) {
        $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
        $col++;
    }
    $rowNumber++;
}

1 个答案:

答案 0 :(得分:0)

请记住,$ col ++不起作用。

这种方式怎么样?:

// Loop through the result set
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
    $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$rowNumber,$row['mail_name'].'@domain.com');
    $objPHPExcel->getActiveSheet()
                ->setCellValue('B'.$rowNumber,$row['account_id']);
    ++$rowNumber;
}