php 导出excel时,字符串中有引号,需要将一个双引号变为两个双引号,这样导出后才能正常显示引号,具体代码如下:
public function export()
{
$result=$this->mkeyword->get_keyword_list();
$keyword_data=$result->result_array();
/* for($i=1;$i<count($keyword_data);$i++)
{
$flag=0;
for($j=0;$j<count($content);$j++)
{
if($content[$j]== $keyword_data[$i]['content'])
{
$keywords[$j][]=$keyword_data[$i]['keywords'];
$flag=1;
break;
}
}
if($flag==0)
{
$content[]=$keyword_data[$i]['content'];
$temp_for=array();
$temp_for[]=$keyword_data[$i]['keywords'];
$keywords[]=$temp_for;
}
}
$keyword_arr=array();
for($i=0;$i<count($content);$i++)
{
$keyword_arr[$i]="";
}
for($i=0;$i<count($content);$i++)
{
$content[$i]="\"".$content[$i]."\"";
}
for($i=0;$i<count($keywords);$i++)
{
for($j=0;$j<count($keywords[$i]);$j++)
{
$keyword_arr[$i]= $keyword_arr[$i].$keywords[$i][$j]."\r"."\n"."-"."\r"."\n";
}
$keyword_arr[$i]="\"".substr($keyword_arr[$i],0,strlen($keyword_arr[$i])-5)."\"";
}*/
$data=array();
for($i=0;$i<count($keyword_data);$i++)
{
$str=str_replace("\"","\"\"",$keyword_data[$i]['keywords']);
$str_content=str_replace("\"","\"\"",$keyword_data[$i]['content']);
$keyword_data[$i]['keywords']="\"".$str."\"";
$keyword_data[$i]['content']="\"".$str_content."\"";
$data[$i]=array($keyword_data[$i]['keywords'],$keyword_data[$i]['content']);
}
ob_end_clean();
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition:attachment;filename="."report".time().".xls");
header("Pragma: no-cache");
header("Expires: 0");
//导出xls 开始
if (!empty($data)){
foreach($data as $key=>$val){
foreach ($val as $ck => $cv) {
$data[$key][$ck]=iconv("UTF-8", "GBK", $cv);
echo $data[$key][$ck];
echo "\t";
}
echo "\n";
}
}
exit();
}