要是导出乱码,加上iconv("UTF-8", "GBK", "你好,中国")
这一句就可以啦。
整体代码如下:
$arr = [
0 => ['get_num' => 12, 'num' => 12, 'update_time' => 20180209],
];
$totalArr = [
'total' => 144,
'people' => 1,
'time' => 20180209,
];
function outerExcel($arr, $totalArr)
{
$file_name = "redPack_{$totalArr['time']}.xls";
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type:application/vnd.ms-excel');
header('Content-Disposition:filename=' . $file_name);
$excel
= '<table width="100%" border="1" cellspacing="1" cellpadding="3" class="tableOutborder">
<tr class="tableHeader">
<td>' . iconv("UTF-8", "GBK", "发出的金币总数") . '</td>
<td> ' . iconv("UTF-8", "GBK", "领取总人数") . '</td>
<td> ' . iconv("UTF-8", "GBK", "日期") . '</td>
</tr>
<tr class="tableHeader">
<td>' . $totalArr['total'] . '</td>
<td>' . $totalArr['people'] . '</td>
<td>' . $totalArr['time'] . '</td>
</tr>
<tr class="tableHeader">
<td>' . iconv("UTF-8", "GBK", "发出的金币数") . '</td>
<td> ' . iconv("UTF-8", "GBK", "领取人数") . '</td>
<td> ' . iconv("UTF-8", "GBK", "日期") . '</td>
</tr>';
foreach ($arr as $value) {
$excel
.= '<tr>
<td>' . $value['get_num'] . '</td>
<td>' . $value['num'] . '</td>
<td>' . $value['update_time'] . '</td>
</tr>';
}
$excel .= '</table>';
echo $excel;
exit;
}
方法二:
当然也可以加上bom
头,不过有些系统不支持,好奇怪。
这里我们只要在文件的开始输出BOM头,告诉windows CSV文件的编码方式,从而让Excel打开CSV时采用正确的编码。
第1种:在所有内容之前输出
print(chr(0xEF).chr(0xBB).chr(0xBF));
第2种:也可以直接写入到csv文件最前端:
$csv_file = chr(0xEF).chr(0xBB).chr(0xBF).$csv_content;
方法二的代码如下:
function outerExcel($arr, $totalArr)
{
print(chr(0xEF).chr(0xBB).chr(0xBF));
$file_name = "redPack_{$totalArr['time']}.xls";
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type:application/vnd.ms-excel');
header('Content-Disposition:filename=' . $file_name);
$excel
= '<table width="100%" border="1" cellspacing="1" cellpadding="3" class="tableOutborder">
<tr class="tableHeader">
<td>发出的金币总数</td>
<td>领取总人数</td>
<td>日期</td>
</tr>
<tr class="tableHeader">
<td>' . $totalArr['total'] . '</td>
<td>' . $totalArr['people'] . '</td>
<td>' . $totalArr['time'] . '</td>
</tr>
<tr class="tableHeader">
<td>发出的金币数</td>
<td>领取人数</td>
<td>日期</td>
</tr>';
foreach ($arr as $value) {
$excel
.= '<tr>
<td>' . $value['get_num'] . '</td>
<td>' . $value['num'] . '</td>
<td>' . $value['update_time'] . '</td>
</tr>';
}
$excel .= '</table>';
echo $excel;
exit;
}
个人推荐有第一个方法。
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
oracle
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接