PHP导出Excle
最简单的方式:
我就直接上例子了:目前UTF-8编码支持国际语言,gbk不支持,国内的wps对乱码问题解决比较好,office还是要导出为gbk
gbk版本:
<?php session_start(); if($_SESSION['admin'] != true){ header("location:login.php"); } require 'init.php'; $data = $dbh->query("SELECT * FROM wine WHERE isDel=0")->fetchAll(); //header("Content-type:application/vnd.ms-excel"); header("Content-Type: application/vnd.ms-excel; charset=gbk"); header("Content-Disposition:attachment;filename=wine.xls"); echo iconv('utf-8', 'gbk', 'Winery Name')."\t"; echo iconv('utf-8', 'gbk', 'Contact person')."\t"; echo iconv('utf-8', 'gbk', 'Phone')."\t"; echo iconv('utf-8', 'gbk', 'Mobile phone ')."\t"; echo iconv('utf-8', 'gbk', 'E-Mail')."\t"; echo iconv('utf-8', 'gbk', 'Company Name')."\t"; echo iconv('utf-8', 'gbk', 'Contact person 2')."\t"; echo iconv('utf-8', 'gbk', 'Phone 2')."\t"; echo iconv('utf-8', 'gbk', 'Mobile phone 2')."\t"; echo iconv('utf-8', 'gbk', 'E-Mail 2')."\t"; echo iconv('utf-8', 'gbk', 'China pay')."\t"; echo iconv('utf-8', 'gbk', 'Spain pay')."\t\n"; foreach($data as $key=>$value){ echo iconv('utf-8', 'gbk', $value['CompanyName'])."\t"; echo iconv('utf-8', 'gbk', $value['Contactperson'])."\t"; echo iconv('utf-8', 'gbk', $value['Phone'])."\t"; echo iconv('utf-8', 'gbk', $value['Mobilephone'])."\t"; echo iconv('utf-8', 'gbk', $value['Mail'])."\t"; echo iconv('utf-8', 'gbk', $value['CompanyName2'])."\t"; echo iconv('utf-8', 'gbk', $value['Contactperson2'])."\t"; echo iconv('utf-8', 'gbk', $value['Phone2'])."\t"; echo iconv('utf-8', 'gbk', $value['Mobilephone2'])."\t"; echo iconv('utf-8', 'gbk', $value['Mail2'])."\t"; echo iconv('utf-8', 'gbk', ($value['ChinaCny']?'yes':'no'))."\t"; echo iconv('utf-8', 'gbk', ($value['Espana']?'yes':'no'))."\t\n"; $current_win_info = unserialize($value['WineInfo']); echo "\t"; echo iconv('utf-8', 'gbk', "Tipo de vino")."\t"; echo iconv('utf-8', 'gbk', "Vintage")."\t";; echo iconv('utf-8', 'gbk', "Name")."\t"; echo iconv('utf-8', 'gbk', "Winery")."\t"; echo iconv('utf-8', 'gbk', "Grapes in %")."\t"; echo iconv('utf-8', 'gbk', "Production")."\t"; echo iconv('utf-8', 'gbk', "Ageing")."\t"; echo iconv('utf-8', 'gbk', "% Alcohol")."\t"; echo iconv('utf-8', 'gbk', "Price")."\t\n"; foreach($current_win_info['typeofwine'] as $key=>$value){ if( $current_win_info['typeofwine'][$key] && $current_win_info['Nombre'][$key] && $current_win_info['retailprice'][$key]){ echo "\t"; echo iconv('utf-8', 'gbk', $current_win_info['typeofwine'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['vintage'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['Nombre'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['winery'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['grapes'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['Production'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['ageing'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['Alcohol'][$key])."\t"; echo iconv('utf-8', 'gbk', $current_win_info['retailprice'][$key])."\t\n"; } } echo "\t\n\t\n"; }
UTF-8
<?php session_start(); if($_SESSION['admin'] != true){ header("location:login.php"); } require 'init.php'; $data = $dbh->query("SELECT * FROM wine WHERE isDel=0")->fetchAll(); //header("Content-type:application/vnd.ms-excel"); header("Content-Type: application/vnd.ms-excel; charset=utf-8"); header("Content-Disposition:attachment;filename=wine.xls"); echo 'Winery Name'."\t"; echo 'Contact person'."\t"; echo 'Phone'."\t"; echo 'Mobile phone '."\t"; echo 'E-Mail'."\t"; echo 'Company Name'."\t"; echo 'Contact person 2'."\t"; echo 'Phone 2'."\t"; echo 'Mobile phone 2'."\t"; echo 'E-Mail 2'."\t"; echo 'China pay'."\t"; echo 'Spain pay'."\t\n"; foreach($data as $key=>$value){ echo $value['CompanyName']."\t"; echo $value['Contactperson']."\t"; echo $value['Phone']."\t"; echo $value['Mobilephone']."\t"; echo $value['Mail']."\t"; echo $value['CompanyName2']."\t"; echo $value['Contactperson2']."\t"; echo $value['Phone2']."\t"; echo $value['Mobilephone2']."\t"; echo $value['Mail2']."\t"; echo ($value['ChinaCny']?'yes':'no')."\t"; echo ($value['Espana']?'yes':'no')."\t\n"; $current_win_info = unserialize($value['WineInfo']); echo "\t"; echo "Tipo de vino"."\t"; echo "Vintage"."\t";; echo "Name"."\t"; echo "Winery"."\t"; echo "Grapes in %"."\t"; echo "Production"."\t"; echo "Ageing"."\t"; echo "% Alcohol"."\t"; echo "Price"."\t\n"; foreach($current_win_info['typeofwine'] as $key=>$value){ if( $current_win_info['typeofwine'][$key] && $current_win_info['Nombre'][$key] && $current_win_info['retailprice'][$key]){ echo "\t"; echo $current_win_info['typeofwine'][$key]."\t"; echo $current_win_info['vintage'][$key]."\t"; echo $current_win_info['Nombre'][$key]."\t"; echo $current_win_info['winery'][$key]."\t"; echo $current_win_info['grapes'][$key]."\t"; echo $current_win_info['Production'][$key]."\t"; echo $current_win_info['ageing'][$key]."\t"; echo $current_win_info['Alcohol'][$key]."\t"; echo $current_win_info['retailprice'][$key]."\t\n"; } } echo "\t\n\t\n"; }