2015-11-04 41 views
0

我正在使用phpexcel将某些数据库值导出为ex​​cel。当我回声的价值一切工作正常,但是当我想出口价值excel例如The Westminster Terrace,Yau Lai Rd
 Hong Kong它只是在Excel中打印相同,而如果我回显它打印The Westminster Terrace, Yau Lai Rd , Hong Kong。 数据库具有utf8_general_ci归类。我使用了下面的代码。Php Excel编码问题

foreach ($data as $question) { 
    $c = $c + 1; 
    //echo $question['their_answer']; 
    $objPHPExcel -> setActiveSheetIndex(0) -> setCellValue($x . $d, $question['their_answer']); 
$x++; 
} 

如何在excel列中打印相同的回显任何想法?
感谢
Somdeb

+0

' '和' '是实体,我认为控制字符。当你说“当我回应价值一切工作正常”,你在浏览器?如果是的话,浏览器正在渲染这些。看看源代码,它会是一样的。 – chris85

+0

yes在浏览器中,但excel中的输出不同 –

+2

@SomdebMukherjee因为浏览器可以解释HTML,并且它们是HTML实体。 Excel不能。 –

回答

2

这些字符的HTML实体。尝试html_entity_decode,像这样:

foreach ($data as $question) { 
    $c = $c + 1; 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($x . $d, html_entity_decode($question['their_answer'])); 
    $x++; 
} 
+0

感谢它工作哇!!!!!!! –

2

这是因为Excel是不是HTML和
是HTML实体(实际上编码回车和换行符)......他们无非是文字字符以上在一个字符串到Excel。你需要将它们转换为使用的东西他们的UTF-8字符像html_entity_decode()

$string = 'The Westminster Terrace,Yau Lai Rd
 Hong Kong'; 
$string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8'); 

你可能也想设置单元格包裹,并且autoheight该行处理这些返回字符以及

0

也许您遇到与实体有关的问题,例如:

尝试解码使用html_entity_decode在运行你的代码是这样的实体:

foreach ($data as $question) { 
    $c = $c + 1; 
    //echo $question['their_answer']; 
    $objPHPExcel -> setActiveSheetIndex(0)->setCellValue($x . $d, html_entity_decode($question['their_answer'])); 
$x++; 
} 

其原因,可能是你的数据库已经保存的事实与实体的数据编码,所以它会正确打印在浏览器中当你echo但不会打印在excel文件中(因为他不明白那些值是什么)

让我知道它是否有效。