基于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");
一点小经验和各位同学分享一下..
,香港空间,香港服务器租用,香港服务器租用

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
