我正在使用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++;
}
答案 0 :(得分:0)
一个选项是在执行SQL查询时排除<p> Select an Answer<p>
,然后从您的选择中选择列和行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>
或者您可以删除包含{{1}}的行,除非您需要它们。