我正在使用 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++;
}