2

我正在使用 PHPExcel 从 MYSQL 数据库中导出数据并将其导入到 excel 文件中。

我有一个看起来像这样的数据库(这也是我使用 PHPExcel 生成它时我的 excel 文件当前的样子):

======================================================
|           Question                  |   Answer     |
=========+===========+===============================|
| Do you listen to music?             |    YES       |            
|----------------------------------------------------|
| Who is your favorite music artists? | Justin Beiber| 
|----------------------------------------------------|
| <p> Select an Answer<p>             |              | 
|----------------------------------------------------|
|Are you a Male or female             |      M       | 
|----------------------------------------------------|

我希望我的 excel 文件看起来像这样:

======================================================
|           Question                  |   Answer     |
=========+===========+===============================|
| Do you listen to music?             |    YES       |            
|----------------------------------------------------|
| Who is your favorite music artists? | Justin Beiber| 
|----------------------------------------------------|
|Are you a Male or female             |      M       | 
|----------------------------------------------------|

我正在使用这段代码:

$objPHPExcel = new PHPExcel();

$col = 1; 
while($row_data = mysql_fetch_assoc($result)) {
    $row = 1;
    if ($col == 1) {
        $row_headings = array_keys($row_data);
        foreach($row_headings as $value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $row++;
        }
        $row = 1;
        $col++;
    }
    foreach($row_data as $value) {
      if (!strstr('<p>', $value)){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);

    $row++;
         }
    }
    $col++;
}
4

1 回答 1

0

一种选择是<p> Select an Answer<p>在执行 SQL 查询时排除您将选择列和行然后从您的选择中DELETE WHERE question = "<p> Select an Answer<p> "

或者如果列Anwser值是null""Question值是时<p> Select an Answer<p>,那么你也可以说:

if($value !== null)
{
  // Output...
} 

例如:

$objPHPExcel = new PHPExcel();

$col = 1; 
while($row_data = mysql_fetch_assoc($result)) {
    $row = 1;
    if ($col == 1) {
        $row_headings = array_keys($row_data);
        foreach($row_headings as $value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $row++;
        }
        $row = 1;
        $col++;
    }
    foreach($row_data as $value) {
        if($value !== null || $value !== '')
        {
         $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
         $row++;
        }
    }
    $col++;
}

或者您可以简单地删除包含的行,<p> Select an Answer<p>除非您需要它们。

于 2012-11-06T02:09:57.803 回答