search
Homephp教程php手册基于PHP导出Excel的小经验 完美解决乱码问题

本篇文章是对PHP导出Excel乱码问题的解决方法就行了详细的分析介绍,需要的朋友参考下

我在PHP项目里要求把数据导出为Excel,并且数据中包含中文.
网上大概了解一下可是使用PHPExcel,可是相对我的需求,这个框架太复杂了.于是还是想找找简单做法.
网上发现其实最简单可以这样写,但问题是这种做法中文的编码不可靠..

复制代码 代码如下:


header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=export_data.xls");
echo "姓名"."\t";
echo "繁體"."\t";
echo "博客"."\t";
echo "\n";
echo "jason"."\t";
echo "@"."\t";
echo "javaeye"."\t";
?>


有些同学会想到header加入字符集

复制代码 代码如下:


header("Content-type:application/vnd.ms-excel;charset=UTF-8");


问题: 这里只是告诉浏览器要选什么字符集查看,最终我的需求还是要生成xls文件.
当然.有些同学还会想到用iconv转码.

复制代码 代码如下:


echo iconv("当前编码","GB18030","此博客来源于javaeye,by jason");


问题: 这样文件里的汉字编码就GB18030,可是Excel这么知道用什么编码打开呢?只能完全依赖OS默认.可是如果碰到繁体BIG5这么办,还是会乱码. 所以还是不靠谱.
最后我采用phpMyAdmin的做法.用HTMLExcel, HTML我们比较熟悉,格式如下.

复制代码 代码如下:


xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">











1234 Robbin会吐口水
5678 javaeye网站





这下可以直接echo了,又不需要iconv转码,只要设置好HTML里的Content-type(这里用的是UTF-8),是不是有舒服的感觉呢? 当然header还是要加上

复制代码 代码如下:


header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=export_data.xls");


一点小经验和各位同学分享一下..
,香港空间,香港服务器租用,香港服务器租用
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools